May 26, 2017

Writing Plugins for GIMP in Python - page 3

Creating a Script

  • April 8, 2009
  • By Akkana Peck
Once you have your plug-in's skeleton in place, it's time to see if it registers right. Quit GIMP if it's already running, re-start it, and see if your plug-in shows up in the menu -- in this case, somewhere under Filters->Distorts (Figure 1).

Problems? If it doesn't show up, make sure your python script is executable, and try running gimp from the commandline. If there are any syntax errors in the script, it'll tell you. If no errors are being printed but your plug-in still isn't showing up, try gimp --verbose and watch to see if it's finding the script.

Writing the plug-in

Once you have your plug-in showing up in the menus, the rest is easy! Just call functions to do the same things you would have done yourself in GIMP. For this short article, let's do something very simple: flip the current image upside down.

How do you figure out how to do that? With the GIMP Procedure browser, available from GIMP's Help menu. In GIMP 2.4 and earlier it was in the Xtns menu inside the Toolbox window.

Open the Procedure Browser and search for flip (Figure 2).

There are a lot of different flip routines there; for now, let's pick one that looks simple, gimp-image-flip.

The Procedure Browser shows that it takes two parameters: the image to flip, and a flip-type that can be ORIENTATION-HORIZONTAL (0) or ORIENTATION-VERTICAL (1).

Here's how that translates into Python:

def python_pytest(img, layer) :
    pdb.gimp_image_flip(img, ORIENTATION_VERTICAL)

Make that change to your pytest file, and try it! No need to restart again -- once you've registered, GIMP knows where the file is and will pick up whatever changes you make. You only need to restart when you change something in the register() function.

So run Filters->Distorts->Py Test, and voil�! The image is flipped upside down (Figure 3). It's that simple.

In the next article, I'll show you how to use plug-in arguments to make a plug-in that does something much more useful.

Akkana Peck is a freelance programmer whose credits include a tour as a Mozilla developer. She's also the author of Beginning GIMP: From Novice to Professional.

Sitemap | Contact Us