【问题标题】:GIMP Python Plugin to load 2 images as layersGIMP Python 插件将 2 个图像加载为图层
【发布时间】:2019-10-11 14:14:42
【问题描述】:

我正在尝试为 gimp 制作一个插件,它将两个图像作为单独的图层打开并转换其中一个(更多内容见下文)。我正在使用 GIMP 2.10.12。

我一直在努力为 GIMP 的 Python 接口找到合适的完整文档,并且主要是根据我能够找到的代码 sn-ps 来工作。这是我目前所拥有的:

#!/usr/bin/env python2
import os

from gimpfu import *

def load_pair(img_f):
    mask_f = img_f.replace(IMG_DIR, PRED_DIR)
    result_f = os.path.splitext(img_f.replace(IMG_DIR, SAVE_DIR))[0]
    result_dir = os.path.dirname(result_f)
    if not os.path.isdir(result_dir):
        os.makedirs(result_dir)

    img = gimp.Image(100, 100)
    pdb.gimp_display_new(img)
    for f, name, pos in ((img_f, "Image", 0), (mask_f, "Mask", 1)):
        layer = pdb.gimp_file_load_layer(img, f)
        pdb.gimp_layer_set_name(layer, name)
        pdb.gimp_image_insert_layer(img, layer, None, pos)


register(
    "python_fu_open_image_pair",
    ...,
    "<Toolbox>/Image/Open Image Pair",
    "",
    [(PF_FILE, "img_f", "Image:", None)],
    [],
    load_pair
)

main()

这种方法可以满足我的要求,但存在一些问题。

问题 1

目前我正在使用gimp.Image(100, 100) 打开一个新图像。这意味着我必须在每次加载新图像对时将画布调整到图层并调整缩放和位置。

有没有办法在打开它之前从 pdb 中找到图像的大小,还是我必须为此使用另一个库(如 PIL)?我正在尝试将插件的依赖项保持在最低限度。

保证两张图片大小相同。

由于 File->Open 会自动将画布调整为图像大小,我希望有一个很好的方法来实现这一点。

问题 2

我想自动创建当前工作文件并将其设置为result_f + '.xcf'(参见上面的代码) - 这样 File -> Save 会自动保存到该文件。这在 pdb 中可行吗?

问题 3

最重要的是,我目前将蒙版图像保存为黑白图像。将蒙版加载为新图层后,我想将黑色转换为透明,将白色转换为绿色(0,255,0)。此外,由于它们保存为 .jpg 图像,因此白色和黑色的强度不一定是 255 和 0,但可以稍微偏离。

如何在我的插件中自动执行此操作?

【问题讨论】:

    标签: python plugins gimp gimpfu


    【解决方案1】:
    1. 最好的方法是正常加载第一个图像,其余的作为附加层。否则,您可以在加载所有图层后重置画布大小 (pdb.gimp_image_resize(...)),然后创建显示。

    2. 您可以通过设置image.nameimage.filename 为图像指定名称和默认文件。

    3. 要将白色转换为绿色,请使用 pdb.plug_in_colors_channel_mixer(...) 并将所有增益设置为 0,除了绿色中的绿色。使黑色透明使用pdb.plug_in_colortoalpha(...)

    PS:对于 color2alpha:

    import gimpcolor
    
    color=gimpcolor.RGB(0,255,0) # green, integer args: 0->255)
    # or
    color=gimpcolor.RGB(0.,1.,0) # green, floating point args (0.->1.)
    
    pdb.plug_in_colortoalpha(image, layer, color)
    

    Python 文档是 Scheme one 的直接副本。在 Python 中,RUN-INTERACTIVE 参数不是位置参数,所以在大多数调用中不会出现,如果需要,它是关键字参数。

    【讨论】:

    • 我已经使 1) 和 2) 工作,但我被困在第 3 点。我找不到如何在我的图层上使用这两个功能。我在developer.gimp.org/api/2.0 找不到任何一个功能,我尝试使用pdb.plug_in_colortoalpha(RUN_NONINTERACTIVE, img, layer, '#000')pdb.plug_in_colortoalpha(RUN_NONINTERACTIVE, img, layer, (0, 0, 0)) 关注oldhome.schmorp.de/marc/pdb/plug_in_colortoalpha.html,但我收到Wrong number of arguments 错误。我还尝试删除 img 参数,但收到 Wrong parameter type 错误。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多