Friday, March 7, 2014

How To Write Python Plug-ins for the GIMP in OS X

I am giving you the lowdown on writing plug-ins for the GIMP (2.8.1) in OS X (Mavericks).  Writing the plug-ins is not hard once you know the ins and outs (which are not well documented on the web).  I spent the better part of a day Googling for answers.  So here is what I learned:

The location of where you put the plug-ins changed when the GIMP came out with a native version for OS X.  I never did get an answer to this by Googling.  I finally just went through each subfolder of GIMP.app until I found it: /Applications/GIMP.app/Contents/Resources/lib/gimp/2.0/plug-ins

There is a very handy tool in the called Python-Fu in GIMP that lets you use Python interactively from within GIMP.  You can find it at Filters->Python-Fu->Console

You can get a list of commands at Help->Procedure Browser.  But before you use a procedure you must:


  •  import gimpfu 
  •  prepend the command with pdb(.) 
  •  change the dashes to underscores 
For example, if you want to use the procedure gimp-file-save, you need to  import gimpfu then change it to pdb.gimp_file_save(parameters)  I really want to thank Hubert and Sebastian Soyer, the authors of the blog "Bits and bobs to remember" for clueing me in on the syntax. 

When Python-Fu calls another plug-in you don't specify a run mode.  If you browse to gimp-file-save in the procedure browser, the documentation lists the parameters as:

  • run-mode - INT32 - The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1), RUN-WITH-LAST-VALS (2) }
  • image - IMAGE - Input image
  • drawable - DRAWABLE - Drawable to save
  • filename - STRING - The name of the file to save the image in
  • raw-filename - STRING - The name as entered by the use
So you might think that you have to enter 5 parameters, right?  Nah.  You skip the run-mode parameter.  So there are only 4 parameters.  By the way,
  • you can get a list of all open images with imageArray = gimp.image_list()
  • then you can get one image by accessing it within the array  img = imageArray[#]
  • you can get the active drawable for the image with imgDrawable = pdb.gimp_image_get_active_drawable(img)
  • You can also obtain the DRAWABLE with imgDrawable = pdb.gimp_image_merge_visible_layers(img, The type of merge { EXPAND-AS-NECESSARY (0), CLIP-TO-IMAGE (1), CLIP-TO-BOTTOM-LAYER (2) })
  • You can get the filename of the image with fn = pdb.gimp_image_get_filename(img)
  • Since this is not interactive with the user, you use the same filename for both parameters
The template for writing the plug-in is well documented on the web. You can find here.  Don't forget to make your plug-in executable with
$ chmod 755 <filename.py>


I hope these hints helped you in some way.  If you have any more insights, please leave a comment.


No comments :