Calling GIMP from Python

Hello!

I would like to achieve the following: I want to apply some GIMP-specific filtering/processing through a Python script.

In particular, I am writing a (Python) application that process a huge amount of pictures. There is a very complex pipeline in which I would like to add a GIMP-specific filtering.

What I am looking for is some sort of API (or a wrapper) through which I could call GIMP in between of my pipeline.

I am open to other routes as well. Do you have any hint? Anything similar youโ€™ve heard of?

Thanks!

Hey, welcome. See Gimp Python Documentation

Otherwise you might look at using GMIC.

Or ImageMagick. Or OpenCV library in Python.

All in all, unless you are using a very specific filter that only exists in Gimp, you have better avoid using Gimp. Many tools in core Gimp are using GEGL and can be used by calling GEGL directly in a command line outside of Gimp, and since in many cases they have no direct equivalent in the scripting API (some are called through a facade that mimics the previous generation of tools, with different parameters, etcโ€ฆ ) if you want to use them you will have to go through hoops to call GEGL from Python).

Now, if you still need to use Gimp, you have two cases:

Running everything from Gimp

Your code is just a Gimp plugin that will iterate images. This can be done in Python (the script-fu has very little access to the underlying OS) but this will be Python v2.17 in the current sable Gimp release.

Running most things outside of Gimp

You code is a regular shell script that at time calls Gimp to execute things. Depending on code complexity all the Gimp code can be passed in the call or Gimp load some secondary file to execute. You can find an example here: cmd - How to run python scripts using gimpfu from windows command line? - Stack Overflow (this is for Windows but you would do the same on Linux or OSX). Code can be Python (2.17!) as in the example or script-fu.

This said, I write Gimp scripts for breakfast but when I have to do batch processing on images my tool of choice is ImageMagick, and if you think the learning curve for IM is steep you have not tried to batch things with Gimp.

What filter would that be?

Some โ€œartisticโ€ filter, for instance, especially if you got enamored to some specific result.

Thank you I will try the approach of running most of things outside GIMP.

Just to give a bit more context: things I want to achieve is overlaying some patches over some target images. I am using other functions to understand patch location and scale. What I am trying to get from GIMP (which I can do manually with point-and-click) is a way to blend/smooth better this overlaid patches so that they look more realistic.

IM supports most blend modes supported by Gimp.