Affiliate Disclosure: By buying the products we recommend, you help keep the lights on at MakeUseOf. Read more.
Photo editing tends to involve a lot of repetitive processes, especially when you’re working with a large album of images. If you’re willing to dabble in scripting, you can use GIMP to automate some of these actions to save yourself time and effort.
Scripting with Python in GIMP isn’t easy, but it’s very rewarding if you’re prepared to learn the ropes. Here’s how to get started with a couple of very basic scripts.
Creating a Python Script
Before we start start working on our project in earnest, we need to lay some foundations. First, open up a text editor, then copy and paste the code below:
#!/usr/bin/python from gimpfu import * def first_plugin(timg, tdrawable): print "Hello, world!" register( "hello_world", "Presents a Hello, World! message", "Presents a Hello, World! message", "Brad Jones", "Brad Jones", "2017", "<Image>/Image/Hello, World!", "RGB*, GRAY*", , , first_plugin) main()
Here’s a brief rundown of what’s going on up there. The first two lines initialize the script and give us access to some helpful libraries. The portion of code following def first_plugin contains the instructions we’re giving GIMP. The information that follows the word register is everything GIMP needs to know about our plug-in.
This is the information we need to give GIMP to register our script:
- Name: the name of the command (e.g. hello_world)
- Blurb: a brief description of the command (e.g. Presents a Hello, World! message)
- Help: the help message to be displayed (e.g. Presents a Hello, World! message)
- Author: the person that created the script (e.g. Brad Jones)
- Copyright: the copyright holder (e.g. Brad Jones)
- Date: the date the script was created (e.g. 2017)
- Label: the way that the script will be referred to in the menu (e.g. <Image>/Image/Hello, World!)
- Parameters: parameters attached to the plug-in (e.g.  — none in this case)
- Results: Results from the plug-in (e.g.  — none in this case)
- Function: the name used to refer to the action in our code (e.g. first_plugin)
Finally, we need to call main().
Save your script, and select All Files from the Save as type dropdown. Make sure to include the .py extension in your file name.
Next, place this file into GIMP’s plug-in folder, which can be found in Windows at Program Files > GIMP 2 > lib > gimp > 2.0 (or ~\Library\Application Support\GIMP\2.8\scripts on a Mac). You may need administrator privileges to do so.
Initialize GIMP and open the Image menu. You should see Hello, World! right there at the bottom.
Now it’s time for us to make our script a little more useful.
Adding Some Functionality
Now we’re going to rewrite our script so that it actually does something practical. Open up the text file once again, and copy and paste the following code:
#!/usr/bin/env python from gimpfu import * def test_script(customtext, font, size): img = gimp.Image(1, 1, RGB) layer = pdb.gimp_text_fontname(img, None, 0, 0, customtext, 10, True, size, PIXELS, font) img.resize(layer.width, layer.height, 0, 0) gimp.Display(img) gimp.displays_flush() register( "python_test", "TEST", "TEST", "Brad Jones", "Brad Jones", "2017", "TEST", "", [ (PF_STRING, "customtext", "Text string", 'Scripting is handy!'), (PF_FONT, "font", "Font", "Sans"), (PF_SPINNER, "size", "Font size", 100, (1, 3000, 1)), ], , test_script, menu="<Image>/File/Create") main()
This is a little bit more complex than our Hello, World! script, but it shares a very similar structure. First we create an image.
img = gimp.Image(1, 1, RGB)
Then we add text based on parameters supplied by the user.
layer = pdb.gimp_text_fontname(img, None, 0, 0, customtext, 10, True, size, PIXELS, font)
Next, we resize the image in accordance with the size of the text.
img.resize(layer.width, layer.height, 0, 0)
Finally, we tell GIMP to display the image on-screen.
All that’s left to do is add the registration information that GIMP needs, with the addition of some parameter settings that we didn’t include earlier.
[ (PF_STRING, "customtext", "Text string", 'Scripting is handy!'), (PF_FONT, "font", "Font", "Sans"), (PF_SPINNER, "size", "Font size", 100, (1, 3000, 1)), ],
Save this just like we saved the Hello, World! script, move it to the plug-ins folder, and restart GIMP. Head to File > Create > TEST to try out our plug-in.
You’ll see a window where you can set various parameters.
Click OK and you’ll create an image that looks something like this.
This demonstrates how you can use scripting in GIMP to automate a process consisting of several different actions. Now let’s write a script that makes changes to an image we already have open.
Inverting a Layer
Once you are comfortable with scripting with Python in GIMP, you can automate all kinds of tweaks to your images. However, we’re going to start as simple as possible by implementing a script that inverts the colors of the current layer.
To get started, open up a text editor again, then copy and paste the following script:
#!/usr/bin/env python from gimpfu import * def invert_current_layer(img, layer): pdb.gimp_invert(layer) register( "python_fu_invert_current_layer", "Invert layer", "Invert colors in the current layer", "Brad Jones", "Brad Jones", "2017", "<Image>/Filters/Custom/Invert current layer", "*", , , invert_current_layer) main()
This follows from the script we created earlier. The first couple of lines of code lay down some foundations, and the last several lines take care of registration. Here is the important section:
def invert_current_layer(img, layer): pdb.gimp_invert(layer)
We’re defining our process, telling GIMP what components we’re going to refer to, then using pdb.gimp_invert to instruct the program to adjust the colors. Save this in the .py file format, add it to the plug-ins folder, then open up GIMP to check that it works.
Navigate to Filters > Custom > Invert current layer.
You should get a result similar to the one above. Of course, it’s already relatively easy to perform an invert operation in GIMP, but this is just a starting point. The great thing about writing your own scripts is that you can create something that’s completely tailored to you.
Next Steps in GIMP Scripting
Once you understand the basics of scripting in GIMP, it’s time to start experimenting. Think about what kind of processes you do a lot and that would be useful to automate. Then comes the tricky part: figuring out how to use code to realize those ideas.
Fortunately, GIMP can offer up some assistance. Navigate to Help > Procedure Browser and you’ll be able to access a list of all the procedures you can utilize.
The Procedure Browser not only lists the procedures themselves, but also gives you information regarding what parameters you need to supply in your code.
You can scroll through the entire list of procedures or use the search bar to narrow the field. Then just insert the procedure name and parameters into your script.
This information will be invaluable as you work on your scripts. Start out with some simple stuff, and before you know it you’ll be making some really useful automated processes!
Do you need help scripting with GIMP? Or do you have a tip that you want to share with other users? Either way, why not join the conversation in the comments section below?
Image Credits: Volkova Vera/Shutterstock