MattsBits
MattsBits

MattsBits

Resources for Creating Python-fu Plugins in GIMP  

by Matt Hawkins, 11/01/2013
Categories : Multimedia : Python

GIMP (The GNU Image Manipulation Program) is a graphics and photo editing application for Linux and Windows. It could be compared to Photoshop but unlike the Adobe product is completely free to use. For this reason I use it exclusively for all my image editing requirements. Why pay for or pirate Photoshop when you can legally use GIMP for free? Most the majority of users GIMP can do everything you need.

A great feature of GIMP is the ability to create your own plugins to automate tasks. These plugins can be created in Script-fu or Python-fu. I tried Script-fu last year and it was awful. The documentation was weak, examples were thin on the ground and the syntax makes no sense whatsoever. Whoever you are your time is not worth wasting on Scipt-fu. I'm sure there is a reason Script-fu is like this (i.e. it's based on 'Scheme') but from the perspective of a normal user it is horribly complicated and messy.

Python LogoLuckily GIMP also supports 'Python-fu'. This is based on Python which is much easier to worth with. Details of Python can be found on www.python.org and there are thousands of tutorials and websites out there to help with it's features, functions and syntax.

As a result I have now managed to create some Python-fu plugins. It isn't all plan sailing so I created this page in an attempt to present some resources I found useful in creating my first plugins.

Online Resources

GIMP Python Documentation
http://www.gimp.org/docs/python/index.html
This page outlines the interfaces to GIMP-Python. It provides an example Python-fu GIMP Plugin which can be used as the basis for a new plugin.

Extending Gimp with Python
http://www.exp-media.com/gimp-python-tutorial
A great site that provides some Python-fu tutorials. These include how to create a new plugin and good explanations of features only vaguely mentioned in the official documentation.

GIMP Plugin Registry
http://registry.gimp.org/
This is the official repository of plugins for GIMP. Before creating your own it is a useful place to look for plugins with similar functionality. You might be able to find a plugin that you can use as a basis for your own creations.

If you are searching Google for resources including "GIMP" and "python-fu" in your search query should result in some useful results. If you are looking for general Python commands then you can just look for general resources using "python".

Example GIMP Python Plugin

Here is a very simple example Python-fu plugin :

#!/usr/bin/env python

# GIMP Plugin - Hello World
# Copyright (c) 2013 Matt Hawkins
# http://www.mattsbits.co.uk

# This is an example Python-fu plugin
# It changes the background and foreground colours to red and green.

from gimpfu import *
  
def python_hello_world(timg, tdrawable):
  g = gimp.pdb

  # Set foreground and background colours
  g.gimp_context_set_foreground((255,0,0))  
  g.gimp_context_set_background((0,255,0))   
  
  # Do other stuff here
  # ...
  # ...

register(
        "python_hello_world",
        "It changes the background and foreground colours to red and green.",
        "It changes the background and foreground colours to red and green.",
        "Matt Hawkins",
        "Matt Hawkins",
        "2013",
        "<Image>/Filters/MattsBits/_Hello_World",
        "RGB*,GRAY*",
        [],
        [],
        python_hello_world)

main()


Download Script

As with all Python scripts the indentation is vitally important! Be-careful when cutting an pasting code. Here is a link to the python_hello_world.py file so you can download that instead.

GIMP LogoThis can be saved as python_hello_world.py and copied to the GIMP plugin directory. On my system this is "C:\Program Files\GIMP 2\lib\gimp\2.0\plug-ins".

The line in the script that reads '"<Image>/Filters/MattHawkins/_Hello_World",' should result in a link to the script appearing under the main "Filters" menu, in a sub-section named "MattsBits".

Author : Matt Hawkins  Last Edit By : Matt Hawkins
PHP Powered  MySQL Powered  Valid XHTML 1.0  Valid CSS  Firefox - Take Back The Web  EUKHost - Recommended Webhosting Solutions

MattHawkins CMS v3.0 - Copyright 2009-2017