我在 GIMP 2.8 中创建了一个带有多个路径的图像,使用Select -> To Path
. 现在我想创建一个 Python 脚本,它将遍历所有这些路径,根据每个路径进行选择,将选择剪切到新图像,做一些额外的事情,并将每个剪切保存到单独的 PNG 图像。
到目前为止,我已经有了这个 Python 脚本,我可以从Filters -> Paths
一个菜单项开始:
#!/usr/bin/env python
# coding: utf-8
from gimpfu import *
import os
def export_paths_to_pngs(img, layer, path, vis):
# get all paths (internally called "vectors")
cnt, vectors = pdb.gimp_image_get_vectors(img)
if (cnt > 0):
# iterate all paths
for n in vectors:
v = gimp.Vectors.from_id(n)
# only visible paths
if (v.visible):
st = v.strokes
sufix = 0
# iterate all strokes in vector
for ss in st:
type, num_pnts, cntrlpnts, closed = pdb.gimp_vectors_stroke_get_points(v, ss.ID)
pdb.gimp_image_select_polygon(img, CHANNEL_OP_REPLACE, len(cntrlpnts), cntrlpnts)
# tell gimp about our plugin
register(
"python_fu_export_paths_to_png",
"Export paths as png files",
"Export paths as png files",
"BdR",
"BdR",
"2017",
"<Image>/Filters/Paths/Export paths to png", # menu path
"", # Create a new image, don't work on an existing one
[
(PF_DIRNAME, "export_directory", "Export destination directory", "/tmp"),
(PF_TOGGLE, "p2", "TOGGLE:", 1)
],
[],
export_paths_to_pngs
)
main()
但是,问题在于 python 函数gimp_image_select_polygon
没有按预期进行选择。选择似乎在贝塞尔曲线(或类似的东西)上有一些差异,这与它所基于的路径并不完全相同。另一方面,菜单项From Path
确实可以正常工作,它会根据路径进行完美选择。见下图:
所以我的问题是:
Select -> From Path
菜单项和 GimpPython 函数有什么区别gimp_image_select_polygon
- 我是否
gimp_image_select_polygon
错误地使用了该功能,还是应该使用其他功能? - 失败了,有没有办法
From Path
直接从 Python 调用菜单项?