【问题标题】:Get sublayers from group layer with Python in Gimp在 Gimp 中使用 Python 从组图层中获取子图层
【发布时间】:2012-04-19 05:02:21
【问题描述】:

我有一个带有嵌套层结构的 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-layerback-layer作为image.layers[0]image.layers[1]image.layers[2]。但 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 从组图层中获取子图层?

【问题讨论】:

    标签: python layer gimp xcf python-fu


    【解决方案1】:

    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 将为层组提供适当的支持 - 你需要上面的 hack 到 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.
    

    【讨论】:

      【解决方案2】:

      感谢您的故障,我在将插件从 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
      

      【讨论】:

      • 您现在可以只检查“层”属性,然后从那里检索子层。请记住,可能有超过 2 层的层,例如包含其他组的组。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多