2

我正在尝试开发一个可以在打开的 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。

4

1 回答 1

2

这是一个陷阱...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 脚本集合。

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

您的修复代码:

#!/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()

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

于 2018-11-08T14:30:41.360 回答