在Gimp中使用Python从组层获取子层

2024-09-27 19:23:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个具有嵌套层结构的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-layercontent-layer和{}作为image.layers[0]image.layers[1]和{}。但是Gimp不能通过列表索引得到content-layer中的子层。在

我可以使用pdb.gimp_image_get_layer_by_name(image, 'content-layer-name-3'),但我不知道层的名称。在

我尝试pdb.gimp_item_get_children(image.layers[1]),但是这个方法返回INT32ARRAY,并且我还没有找到如何通过其id检索项

如何在Gimp(2.8)中使用Python从组层获取子层?在


Tags: 文件nameimagelayergetlayerscontent结构
2条回答

在这个开发周期中,gimppython基本上没有得到维护(您可以将这大部分归咎于我自己)。在

完成的少数更新之一是创建“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'>
>>> 

**更新**

我花了一些黑客时间,gimp2.8final将提供对层组的适当支持-你将需要以上的黑客到gimp2.8rc1,但如果你现在从git master构建项目,层组显示为“GroupLayer”的实例,并具有“layers”属性,其工作方式与图像中的“layers”属性类似。在

commit 75242a03e45ce751656384480e747ca30d728206

^{pr2}$

感谢你的失败,我正在努力解决同样的问题,因为我正在更新我的插件从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

相关问题 更多 >

    热门问题