我有一个带有嵌套图层结构的XCD文件:
image
front-layer
content-layer
content-layer-name-1
content-layer-name-2
content-layer-name-3
back-layer
我使用image = pdb.gimp_file_load(xcf_file, xcf_file)
打开文件,可以将front-layer
,content-layer
和back-layer
设为image.layers[0]
,image.layers[1]
和{{1} }。但是Gimp无法通过列表索引在image.layers[2]
中获得子层。
我可以使用content-layer
,但我不知道图层的名称。
我尝试pdb.gimp_image_get_layer_by_name(image, 'content-layer-name-3')
,但此方法会返回pdb.gimp_item_get_children(image.layers[1])
项目的子项列表,但我还没有找到如何通过其ID检索项目。
如何在Gimp(2.8)中使用Python从组图层获取子图层?
答案 0 :(得分:8)
GIMP Python在这个开发周期中大部分都没有维护(你可以把很多责任归咎于我自己)。
为数不多的更新之一是创建了“Item”类 - 并在其上实现了一个类方法,允许用户使用PDB方法返回的数字ID来检索项目。
所以,你可以使用,就像你发现pdb.gimp_item_get_children(group_layer)
一样
子项的返回ID使用gimp.Item.from_id
来检索实际图层。
这是一个GIMP控制台部分,我在那里“手动”检索子图层:
>>> img = gimp.image_list()[0]
>>> c = img.layers[0]
>>> c
<gimp.Layer 'Layer Group'>
>>> pdb.gimp_item_get_children(c)
(1, (4,))
>>> c2 = gimp.Item.from_id(4)
>>> c2
<gimp.Layer 'cam2'>
>>>
** 更新 **
我花了一些黑客时间,GIMP 2.8 final将对Layer Groups提供适当的支持 - 你需要上面的gimp 2.8 RC 1,但如果你现在从git master构建项目,图层组显示作为“GroupLayer”的实例,并具有“layers”属性,其功能与图像中的“layers”属性相同。
commit 75242a03e45ce751656384480e747ca30d728206
Date: Fri Apr 20 04:49:16 2012 -0300
pygimp: adds proper support for layer groups
Layer groups where barely supported using numeric IDs and
by calling gimp.Item.from_id. This adds a Python
GroupLayer class.
答案 1 :(得分:0)
感谢分解,我正在努力解决同样的问题,因为我正在将插件从2.6更新到2.7~2.8。这是编辑过的功能:
def find_layer_by_name (image, name):
for layer in image.layers:
#check if layer is a group and drill down if it is
if pdb.gimp_item_is_group(layer):
gr = layer
gr_items = pdb.gimp_item_get_children(layer)
for index in gr_items[1]:
item = gimp.Item.from_id(index)
if item.name == name:
return item
# if layer is on the first level
if layer.name == name:
return layer
return None