[英]How to get a layer with python in gimp?
I want to get a specific layer in gimp with python.我想用 python 在 gimp 中获得一个特定的层。
I have the image with two layers.我有两层图像。 I found the command the num_layers, layer_ids = pdb.gimp_image_get_layers(image)
我找到了命令num_layers, layer_ids = pdb.gimp_image_get_layers(image)
when I run the command I get the output当我运行命令时,我得到 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)
.我找到了命令layer = pdb.gimp_image_get_layer_by_name(image, name)
和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.如果你想确定你的脚本/插件应该在哪一层工作,插件注册应该声明以 PF_IMAGE 和 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.您还可以使用image.active_layer
检索活动层,但这是危险的,您的插件可能已在遮罩或通道上调用,通常使用前面的方法是这样做的方法。 This method has its uses when trying things in the Python-fu console.在 Python-fu 控制台中尝试操作时,此方法有其用处。
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.如果您在脚本中创建了图层,则该图层已由 API 调用返回,因此请将其保存在变量中以便稍后重用。
For a specific layer, it will depend on the criteria, you can对于特定层,这将取决于标准,您可以
filter by name: [l from image.layers if l.name='XXX'][0]
,按名称过滤: [l from image.layers if l.name='XXX'][0]
,
filter on position in the stack:在堆栈中过滤 position:
image.layers[0]
for the top, image.layers[0]
为顶部,image.layers[-1]
for the "Background" at the bottom. image.layers[-1]
用于底部的“背景”。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)
).如果你想从之前的处理中检索一个层,你可以“纹身”它( pdb.gimp_item_set_tattoo(item, tattoo)
其中tattoo
是你选择作为签名的 integer ),然后在后续处理中使用pdb.gimp_image_get_layer_by_tattoo(image, tattoo)
检索它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.纹身将作为图像的一部分保存,因此将在作品 session 之后继续存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.