我的目标是将一个图像放在另一个图像上,并将生成的图像保存为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:断言'域名 != 0'失败回溯(最近一次呼叫最后):文件 “/usr/lib/gimp/2.0/python/gimpfu.py”,第821行,在_run返回 apply(func,params [1:])File“/usr/lib/gimp/2.0/plug-ins/my.py”, 第31行,在规模finalLayer = pdb.gimp_image_merge_down(newImage,tdraw,1)RuntimeError:执行 错误批处理命令遇到执行错误:错误:(: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)导致原始 png 图像作为以 jpg 格式存储的输入
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)
答案 0 :(得分:1)
此:
tdraw = pdb.gimp_layer_new_from_visible(timg, newImage, 'logo')
newImage.add_layer(tdraw, 1)
不会将图层添加为顶层,而是添加现有顶层。如果它是第二层,那么它就成了底层。在这种情况下,merge_down
无法合并。