【问题标题】:GIMP Python - Fill Path/Vector with colorGIMP Python - 用颜色填充路径/矢量
【发布时间】:2018-11-07 20:48:00
【问题描述】:

我正在尝试开发一个可以在打开的 SVG 文件上运行的脚本。我想遍历所有路径并用任意颜色填充路径(稍后我将替换这部分代码)。第一阶段只是遍历路径,我似乎无法弄清楚如何做到这一点。我的代码如下 - 为什么我没有看到任何被迭代的路径?

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from gimpfu import *

def plugin_main(image, layer, path):
    vectors_count, vectors = pdb.gimp_image_get_vectors(image)
    for n in vectors:
        pdb.gimp_image_select_item(image,CHANNEL-OP-REPLACE,n)
        foreground = pdb.gimp_context_get_foreground()
        pdb.gimp_edit_fill(image.layers[0], foreground)

register(
    "create_polygon_art",
    "Fills all the paths with the average color within path",
    "Fills all the paths with the average color within path",
    "Bryton Pilling",
    "Bryton Pilling",
    "2018",
    "<Image>/Filters/Fill all paths with average color",
    "RGB*, GRAY*",
    [],
    [],
    plugin_main
)

main()

我还尝试了一些通过谷歌搜索找到的不同方法,包括使用更简单的方法进行迭代,例如:

for v in gimp.Vectors

但无论我尝试什么,我似乎都无法获得路径迭代的证据。

我在 Windows 10 64 位上使用 gimp 2.10.6。

【问题讨论】:

    标签: python gimp gimpfu


    【解决方案1】:

    这是一个陷阱...pdb.gimp_image_get_vectors(image) 返回路径的整数 ID 列表,但后面的调用需要 gimp.Vectors 对象。

    image.vectors 确实是gimp.Vectors 的列表,您可以使用

    迭代所有路径
    for vector in image.vectors:
    

    更多问题:

    • 您在 register() 中声明了两个参数,但在您的函数中有三个。在实践中,您不需要路径参数,因为无论如何您都要迭代它们。
    • 函数的 layer 参数是调用插件时的活动层,通常是您要绘制的层
    • gimp-edit-fill 采用颜色源而不是颜色。当您进一步使用代码时,您必须设置前景色,并推送/弹出上下文
    • CHANNEL-OP-REPLACE 不是有效的 Python 符号,在 Python 中您应该使用 CHANNEL_OP_REPLACE(带下划线)

    两个python脚本集合herethere

    如果您在 Windows 下,调试脚本的一些提示here

    您的代码修复:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    from gimpfu import *
    
    def plugin_main(image, layer):
        for p in image.vectors:
            pdb.gimp_image_select_item(image,CHANNEL_OP_REPLACE,p)
            pdb.gimp_edit_fill(layer, FOREGROUND_FILL)
    
    register(
        "create_polygon_art",
        "Fills all the paths with the average color within path",
        "Fills all the paths with the average color within path",
        "Bryton Pilling",
        "Bryton Pilling",
        "2018",
        "<Image>/Test/Fill all paths with average color",
        "RGB*, GRAY*",
        [],
        [],
        plugin_main
    )
    
    main()
    

    您可以通过绘制“笔画”来使您的代码更加用户友好(这样您就可以拥有一个包含多个笔画的路径)。如果您想要对笔划进行单独选择,可以将它们复制到临时路径。这方面的代码可以在上面集合的一些脚本中找到。

    【讨论】:

    • 谢谢!我发现需要下划线的 CHANNEL_OP_REPLACE 导致脚本无法工作,修复这意味着脚本运行。提到的其他几点非常有帮助,谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-10
    • 2015-06-02
    • 1970-01-01
    • 2013-07-23
    • 2021-07-05
    • 1970-01-01
    相关资源
    最近更新 更多