Code Jamming Everyday IT problems, solutions and discussions

Simple Gimp plugin for batch processing in Python

Not so far ago I needed to batch-process some images. The task was to resize them so they fit to given smallest resolution. Although Gimp has some batch-processing plugins, I wasn’t able to solve my problem with them. That’s why I’ve invented small little bike and I’d like to share with you workarounds and explanations of some Gimp plug-in development issues.

Your plugin can take as little as just one python file with call to function register (from gimpfu) and passing to it some metadata and actual method of plugin. You can read more about parameters to register on official docs website. But a few moments still need clarifications.

As official documentation says,

If the plugin is to be run on an image, the first parameter to the plugin function should be the image, and the second should be the current drawable

 And if you’ll not change one of the parameters of register function, which is called image_types, your plugin will be disabled without an image opened in GIMP. If you don’t know what to write there, just leave it blank (other possible values are “RGB*”, “GRAY*”).

Other parameters of register function set actual parameters of you plugin main method with default values.

Sample call to register (you can find tons of them on the internet):

register(
    "python_fu_resize_max",
    "Scales the image to fit minimal size in megapixels",
    "Help here",
    "Your Name",
    "Your Name",
    "2014",
    "<Toolbox>/Tools/Resize images to min size...",
    "",
    [
        (PF_INT, "min_size", "Minimum image size", 6000000),
        (PF_BOOL, "copy", "Make a JPEG copy", TRUE),
        (PF_BOOL, "process_dir", "Process a directory", TRUE),
        (PF_DIRNAME, "path", "Directory to Open", "./"),
    ],
    [],
    plugin_main)`

And declaration of plugin_main:

def plugin_main(minsize=6000000, savecopy=TRUE, processdir=TRUE, dirname="./"):

Don’t forget to make the script executable itself.

You can find complete example here.

Buy me a coffeeBuy me a coffee
comments powered by Disqus