Oct 25, 2009

Running GIMP Python plugins in batch mode

In the previous entry I wrote about creating GIMP plugins in Python. This follows up on that by showing the bit of magic for running one of these plugins in batch mode from the command line. You need to provide values for each positional parameter defined for the plugin. It helps if the plugin has been designed with batch mode in mind.
gimp -ib '(python-fu-mylogo-batch RUN-NONINTERACTIVE "in.jpg" "out.jpg" "Made by me" 50 "Sans" 24)(gimp-quit 1)'
The language used is TinyScheme, I don't know how to use Python in this mode, but it matters very little. Any function defined in the PDB can be called including of course Python functions. Add --verbose to the command line to see the execution flow. As a side note, you can use the following command to start the PDB browser directly without going through the GIMP UI:
gimp -ib '(plug-in-dbbrowser RUN-INTERACTIVE)(gimp-quit 1)'

To make a batch mode function, add a function that opens the image, calls the main plugin function and then saves the result. Register this function also, probably under "<Image>/Tools" this time.
def mylogo_batch(infile, outfile, text, opacity, fontname, fontsize):
img = pdb.gimp_file_load(infile, infile)
pdb.python_fu_mylogo(img, text, opacity, fontname, fontsize)
pdb.gimp_image_flatten(img)
drawable = pdb.gimp_image_get_active_layer(img)
pdb.gimp_file_save(img, drawable, outfile, outfile)
pdb.gimp_image_delete(img)

The batch plugin can be invoked from the UI also, a file selection dialog will prompt the user to choose the input and output and so on.

No comments:

Post a Comment

Please keep your message on topic if at all possible...