【问题标题】:Gimp, python-fu: RuntimeError: execution error in pdb.gimp_image_merge_downGimp,python-fu:RuntimeError:pdb.gimp_image_merge_down 中的执行错误
【发布时间】:2023-04-05 05:50:01
【问题描述】:

我的目标是将一张图像放在另一张图像上并将生成的图像保存为 jpeg 文件。
第一个是新的,它有 450x300 尺寸和白色背景。第二个是从文件加载的,其可见部分作为图层添加到第一个。

我已将以下 python 脚本添加到 /usr/lib/gimp/2.0/plug-ins 文件夹

#!/usr/bin/python
import os

from gimpfu import *
import gimpfu

import logging
def scale(timg, tdrawable, imageName):
    logger = logging.getLogger()
    logger.info('got logo file {0}'.format(imageName))

    newImage = pdb.gimp_image_new(450, 300, RGB)

    newLayer = pdb.gimp_layer_new(newImage, 450, 300, 0, "background", 100.0, NORMAL_MODE)

    pdb.gimp_context_set_background((255, 255, 255))
    pdb.gimp_drawable_fill(newLayer, gimpfu.BACKGROUND_FILL)
    logger.info('Created new background image {0}')
    newImage.add_layer(newLayer, 0)

    timg = pdb.gimp_file_load(imageName, imageName)

    tdraw = pdb.gimp_layer_new_from_visible(timg, newImage, 'logo')      
    newImage.add_layer(tdraw, 1)
    finalLayer = pdb.gimp_image_merge_down(newImage,tdraw,1)
    fileNameNoExt = os.path.splitext(imageName)[0]
    pdb.file_jpeg_save(newImage, finalLayer, fileNameNoExt + '.jpg', fileNameNoExt + '.jpg', 1.0, 0.0, 1, 1, '', 0, 0, 0, 0)

通过运行它

gimp --no-interface -b '(python-fu-my RUN-NONINTERACTIVE 0 0 "logo.png")' -b '(gimp-quit 0)'

返回

(gimp:4224): GLib-CRITICAL **: g_error_new_literal: assertion 'domain
!= 0' 回溯失败(最近一次调用最后一次):文件
“/usr/lib/gimp/2.0/python/gimpfu.py”,第 821 行,在 _run 返回
应用(函数,参数[1:])文件“/usr/lib/gimp/2.0/plug-ins/my.py”,
第 31 行,按比例 finalLayer =
pdb.gimp_image_merge_down(newImage,tdraw,1) RuntimeError: execution
错误批处理命令遇到执行错误:错误:(:1)
python-fu-my的程序执行失败

怎么了?如何正确地做,了解问题的根本原因。

编辑。
遵循 xenoid 的建议:
1) 使用 gimp_image_merge_down 会导致 logo.jpeg 的白色背景为 450、300 大小

def my(timg, tdrawable, imageName):
    logger = logging.getLogger()
    logger.info('got logo file {0}'.format(imageName))

    newImage = pdb.gimp_image_new(450, 300, RGB)

    newLayer = pdb.gimp_layer_new(newImage, 450, 300, 0, "background", 100.0, NORMAL_MODE)

    pdb.gimp_context_set_background((255, 255, 255))
    # pdb.gimp_context_set_foreground((255, 255, 255))
    pdb.gimp_drawable_fill(newLayer, gimpfu.BACKGROUND_FILL)
    logger.info('Created new background image {0}')
    newImage.add_layer(newLayer, 0)

    logger.info('loading logo {0}'.format(imageName))
    logo = pdb.gimp_file_load(imageName, imageName)
    # logoDrawable = pdb.gimp_image_get_active_layer(logo)

    #
    visibleLayer = pdb.gimp_layer_new_from_visible(logo, newImage, 'logo')
    newImage.add_layer(visibleLayer, 1)
    mergedL = pdb.gimp_image_merge_down(newImage, newLayer, 0)

    fileNameNoExt = os.path.splitext(imageName)[0]
    pdb.file_jpeg_save(newImage, mergedL, fileNameNoExt + '.jpg', fileNameNoExt + '.jpg', 1.0, 0.0, 1, 1, '', 0, 0, 0, 0)

2) 使用 newImage.add_layer(visibleLayer, 0) 会得到以 jpg 格式存储的原始 png 图像作为输入存储

def scale(logo, tdrawable, imageName):
    logger = logging.getLogger()
    logger.info('got logo file {0}'.format(imageName))

    newImage = pdb.gimp_image_new(450, 300, RGB)

    newLayer = pdb.gimp_layer_new(newImage, 450, 300, 0, "background", 100.0, NORMAL_MODE)

    pdb.gimp_context_set_background((255, 255, 255))
    # pdb.gimp_context_set_foreground((255, 255, 255))
    pdb.gimp_drawable_fill(newLayer, gimpfu.BACKGROUND_FILL)
    logger.info('Created new background image {0}')
    newImage.add_layer(newLayer, 0)

    logger.info('loading logo {0}'.format(imageName))
    logo = pdb.gimp_file_load(imageName, imageName)
    # logoDrawable = pdb.gimp_image_get_active_layer(logo)

    #
    visibleLayer = pdb.gimp_layer_new_from_visible(logo, newImage, 'logo')
    newImage.add_layer(visibleLayer, 0)
    # mergedL = pdb.gimp_image_merge_down(newImage, newLayer, 0)

    fileNameNoExt = os.path.splitext(imageName)[0]
    pdb.file_jpeg_save(newImage, visibleLayer, fileNameNoExt + '.jpg', fileNameNoExt + '.jpg', 1.0, 0.0, 1, 1, '', 0, 0, 0, 0)

【问题讨论】:

  • 你写的东西不应该放在 /usr/lib/gimp/2.0/plug-ins 中,而应该放在 ~/.gimp-2.8/plug-ins 中(注意前面的点,告诉探索显示隐藏文件)。或者你把它放在一些更“可见”的目录中,然后将该目录添加到Edit>Preferences>Folders>Plug-ins。您还可以在 ~/.gimp-2.8/plug-ins 中添加指向文件实际位置的软链接。

标签:
python
ubuntu
gimp
python-fu
gimpfu