简体   繁体   中英

How to get a layer with python in gimp?

I want to get a specific layer in gimp with python.

I have the image with two layers. I found the command the num_layers, layer_ids = pdb.gimp_image_get_layers(image)

when I run the command I get the output

num_layers = 2
layer_ids  = (5, 4)

I found the commands layer = pdb.gimp_image_get_layer_by_name(image, name) and layer = pdb.gimp_image_get_layer_by_tattoo(image, tattoo) .

How do I get a specific layer?

  • If you want to determine on which layer your script/plugin should work, the plugin registration should declare parameters that start with with a PF_IMAGE and a PF_DRAWABLE. These will be set to the image and active layer when the script is called.

  • You can also retrieve the active layer using image.active_layer but this is dangerous, your plugin could have been called on a mask or channel, using the previous method is normally the way to do it. This method has its uses when trying things in the Python-fu console.

  • If you created the layer in your script, the layer has been returned by the API call, so keep it in a variable to reuse it later.

  • For a specific layer, it will depend on the criteria, you can

    • filter by name: [l from image.layers if l.name='XXX'][0] ,

    • filter on position in the stack:

      • image.layers[0] for the top,
      • image.layers[-1] for the "Background" at the bottom.
    • You can also find the first visible or linked.

    But doing so is usually a bad idea.

  • If you want to retrieve a layer from a previous processing, you can "tattoo" it ( pdb.gimp_item_set_tattoo(item, tattoo) where tattoo is an integer that you pick as your signature), and then retrieve it in a subsequent processing using pdb.gimp_image_get_layer_by_tattoo(image, tattoo) ). The tattoo will be saved as part of the image and so will survive beyond a work session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM