RSS/Atom feed Twitter
Site is read-only, email is disabled

How to Delete Layer in Python Script

This discussion is connected to the mailing list which is provided by the GIMP developers and not related to

This is a read-only list on so this discussion thread is read-only, too.

2 of 2 messages available
Toggle history

Please log in to manage your subscriptions.

How to Delete Layer in Python Script ericbright2002 07 Jun 17:35
  How to Delete Layer in Python Script ericbright2002 08 Jun 15:45
2020-06-07 17:35:43 UTC (almost 3 years ago)

How to Delete Layer in Python Script


I'm new to scripting in Gimp with python, but I've had a lot of success in the past two days. Here's the overview:

I'm using Gimp to mock-up prototypes of card games. (I know I'll be told to use another program, but I like Gimp!) I have tons of text boxes on the cards with some needing rotated 180 degrees, icons, and labels. I have a python script that reads everything it needs from a CSV, populates the text boxes, changes card borders, adjusts icons, and even makes new layer of text and rotates them 180 as needed. It then merges everything down into a new image, saves the image in a directory, deletes the fresh image, and starts the process again for the next card. All fine and good.

However, after rotating the text box, I can't delete the layer. Since I'm making 100 cards with 3 places of rotated text, I figure having 300 new layers adding to the image would cause some bad things to happen.

Specifically, during the process, I create a new text layer, add the necessary text, name it 'rotatedNoun', calculate the center of rotation, and rotate it 180 degrees (using math.pi and radians). After saving the new image, I want to delete the layer 'rotatedNoun' to prevent 300 new layers from stacking up and crashing Gimp.

I have tried the following:

1) pdb.gimp_item_delete(rotatedNoun)

I know this won't work because the description of the function says that the layer can't already be added to the image.

2) pdb.gimp_image_remove_layer(image, 'rotatedNoun')

This method works if I use it in the Python console, but when I try using it in the script, the script will not load when Gimp is started. I know it is this specific line because when I comment it out, the script loads just fine in Gimp. Not sure why it works in the console but not as part of the script?

3) image.remove_layer('rotatedNoun')

Again, this method works in the Python console but not in the script because the script won't load when Gimp is started. The same thing happens in that when I comment out this single line, the script will load fine.

This is the last piece of my crazy puzzle, and I'd appreciate any help that you can offer. Thank you!

2020-06-08 15:45:43 UTC (almost 3 years ago)

How to Delete Layer in Python Script


I figured out a work-around and now have everything working as I want. Yay! To make it work, I moved the line of code from one function to another.

Original set-up was trying to delete the layer after saving the new image (at the same try I reset layer visibility for all other layers). That's where it wouldn't load into Gimp at all.

I moved the delete layer command to the start of the function that rotated my text and it worked there. So happy!

However, I'm still confused as to why it didn't work as I originally had it coded. It seems like I should be to tell it to delete a layer anywhere in the code.