Not a very common topic for me, but I thought it could be neat to mention some tips & tricks. I won’t go into the details of the Python GIMP SDK, most of it can be figured out from the GIMP documentation. I spent a total of one hour researching this topic, so I’m not an expert and I could have made mistakes, but perhaps I can save some effort to others which want to achieve the same results. You can jump to the end of the tutorial to find a nice skeleton batch script if you’re not interested in reading the theory.
To those wondering why GIMP, it’s because I created a new icon for Profiler and wanted to automatize some operations on it in order to have it in all sizes and flavors I need. One of the produced images had to be semi-transparent. So I thought, why not using a GIMP batch command, since anyway GIMP is installed on most Linux systems by default?
Just to mention, GIMP supports also a Lisp syntax to write scripts, but it caused my eyes to bleed profusely, so I didn’t even take into it consideration and focused directly on Python.
Of course, I could’ve tried other solutions like PIL (Python Imaging Library) which I have used in the past. But GIMP is actually nice, you can do many complex UI operations from code and you also have an interactive Python shell to test your code live on an image.
For example, open an image in GIMP, then open the Python console from Filters -> Python-Fu -> Console and execute the following code:
img = gimp.image_list() img.layers.opacity = 50.0
And you’ll see that the image is now halfway transparent. What the code does is to take the first image from the list of open images and sets the opacity of the first layer to 50%.
This is the nice thing about GIMP scripting: it lets you manipulate layers just like in the UI. This allows for very powerful scripting capabilities.
The first small issue I’ve encountered in my attempt to write a batch script, is that GIMP only accepts Python code as command line argument, not the path to a script on disk. According to the official documentation:
GIMP Python All this means that you could easily invoke a GIMP Python plug-in such as the one above directly from your shell using the (plug-in-script- fu-eval …) evaluator:
gimp –no-interface –batch ‘(python-fu-console-echo RUN-NONINTERACTIVE “another string” 777 3.1416 (list 1 0 0))’ ‘(gimp-quit 1)’
The idea behind it is that you create a GIMP plugin script, put it in the GIMP plugin directory, register methods like in the following small example script:
#! /usr/bin/env python from gimpfu import * def echo(*args): """Print the arguments on standard output""" print "echo:", args register( "console_echo", "", "", "", "", "", "
/Xtns/Languages/Python-Fu/Test/_Console Echo", "", [ (PF_STRING, "arg0", "argument 0", "test string"), (PF_INT, "arg1", "argument 1", 100 ), (PF_FLOAT, "arg2", "argument 2", 1.2 ), (PF_COLOR, "arg3", "argument 3", (0, 0, 0) ), ], , echo ) main()
And then invoke the registered method from the command line as explained above.
I noticed many threads on stackoverflow.com where people were trying to figure out how to execute a batch script from the command line. Now, the obvious solution which came to my mind is to execute Python code from the command line which prepends the current path to the sys.path and then to import the batch script. So I searched and found that solution suggested by the user xenoid in this stackoverflow thread.
So the final code for my case would be:
gimp-console -idf --batch-interpreter python-fu-eval -b "import sys;sys.path=['.']+sys.path;import batch;batch.setOpacity('appicon.png', 50)" -b "pdb.gimp_quit(1)"
from gimpfu import * def setOpacity(fname, level): img = pdb.gimp_file_load(fname, fname) img.layers.opacity = float(level) img.merge_visible_layers(NORMAL_MODE) pdb.file_png_save(img, img.layers, fname, fname, 0, 9, 1, 0, 0, 1, 1) pdb.gimp_image_delete(img)
What took me most to understand was to call the method merge_visible_layers before saving the image. Initially, I was trying to do it without calling it and the saved image was not transparent at all. So I thought the opacity was not correctly set and tried to do it with other methods like calling gimp_layer_set_opacity, but without success.
I then tried in the console and noticed that the opacity is actually set correctly, but that that information is lost when saving the image to disk. I then found the image method flatten and noticed that the transparency was retained, but unfortunately the saved PNG background was now white and no longer transparent. So I figured that there had to be a method to obtain a similar result but without losing the transparent background. Looking a bit among the methods in the SDK I found merge_visible_layers. I think it’s important to point this out, in case you experience the same issue and can’t find a working solution just like it happened to me.
Now we have a working solution, but let’s create a more elegant one, which allows use to use GIMP from within the same script, without any external invocation.
#!/usr/bin/env python def setOpacity(fname, level): img = pdb.gimp_file_load(fname, fname) img.layers.opacity = float(level) img.merge_visible_layers(NORMAL_MODE) pdb.file_png_save(img, img.layers, fname, fname, 0, 9, 1, 0, 0, 1, 1) pdb.gimp_image_delete(img) # GIMP auto-execution stub if __name__ == "__main__": import os, sys, subprocess if len(sys.argv) < 2: print("you must specify a function to execute!") sys.exit(-1) scrdir = os.path.dirname(os.path.realpath(__file__)) scrname = os.path.splitext(os.path.basename(__file__)) shcode = "import sys;sys.path.insert(0, '" + scrdir + "');import " + scrname + ";" + scrname + "." + sys.argv + str(tuple(sys.argv[2:])) shcode = "gimp-console -idf --batch-interpreter python-fu-eval -b \"" + shcode + "\" -b \"pdb.gimp_quit(1)\"" sys.exit(subprocess.call(shcode, shell=True)) else: from gimpfu import *
We can now call our function simply like this:
./batch.py setOpacity appicon.png 50.0
Which looks very pretty to me.
I could go on showing other nice examples of image manipulation, but the gist of the tutorial was just this. However, GIMP has a rich SDK which allows to automatize very complex operations.
7 thoughts on “Batch image manipulation using Python and GIMP”
Thank you Daniel for sharing your findings 🙂
I know almost nothing about python but, yes, it’s used pretty everywhere … so I should start taking a look at it seriously. 🙂
Thanks for the skeleton code … it might be useful whenever I had to use GIMP 😉
Thank you tony, it’s a small thing, but, as you said, it might be useful someday 🙂
Thanks, that’s great – helped to fill in some handy pieces.
To add/retain transparency you add an alpha channel to a layer:
Otherwise, transparent pixels are filled with black or the background color.
Hello G, glad it was useful. Many thanks for the information!
Since I went to Linux, code is so visible, Python looks OK since I should get a language started. That comment about Lisp made me laugh, saw some elsewhere. Love the Gimp, hmm batches.
tried it, it did not work? what did i do wrong?
GIMP 2.8.4 Python Console
Python 2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
Traceback (most recent call last):
File “”, line 1, in
TypeError: type mismatch
it expects a float and not an integer. Just write the following instead:
Don’t forget the .0, otherwise it will interpret it as an int.