我使用Select -> To Path
在GIMP 2.8中创建了一个包含多条路径的图像。现在我想创建一个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
?答案 0 :(得分:1)
不要使用
pdb.gimp_image_select_polygon(...)
用于创建多边形选择(如&#34;多边形&#34;模式中的手绘选择)。你应该使用
pdb.gimp_image_select_item(image,CHANNEL_OP_REPLACE,path)
(在整个路径上,无需按行程划分)。
PS:你在路径上迭代的代码是非常人为的。它通常很简单:
for v in img.vectors:
if v.visible: