Programming Gimp Plugins with Python
In this article we look at a simple Hello World of a Gimp plugin in Python. For editing I can recommend the development environment PyCharm. This can be downloaded from https://www.jetbrains.com/de-de/pycharm/.
Copy the following source code into a new file and save it as helloWorld.py:
#!/usr/bin/env python
import gtk
from gimpfu import *
def helloWorldFn() :
# Using gtk to display an info type message see -> http://www.gtk.org/api/2.6/gtk/GtkMessageDialog.html#GtkMessageType
message = gtk.MessageDialog(type=gtk.MESSAGE_INFO, buttons=gtk.BUTTONS_OK)
message.set_markup("Hello World \nThis Dialog Will Cause Unexpected Issues During Batch Procedures")
message.run()
message.destroy()
# Using GIMP's interal procedure database see -> http://docs.gimp.org/en/glossary.html#glossary-pdb
pdb.gimp_message("Hello World, This Message Looks Like An Error And/Or Warning")
# Using stdout see -> https://en.wikipedia.org/wiki/Standard_streams#Standard_output_.28stdout.29
print "Hello World \nThis message does not show in the GUI."
# (Unix Terminal Output) Will not work on Windows Based Systems.
return
# see -> http://www.gimp.org/docs/python/
register(
#name
"helloWorldPlugin",
#blurb
"Saying Hello World",
#help
"Saying Hello to the World",
#author
"William Crandell <william@crandell.ws>",
#copyright
"William Crandell <william@crandell.ws>",
#date
"2015",
#menupath
"Hello World",
#imagetypes (use * for all, leave blank for none)
"",
#params
[],
#results
[],
#function (to call)
helloWorldFn,
#this can be included this way or the menu value can be directly prepended to the menupath
menu = "<Toolbox>/Hello/")
main()
For a Gimp plugin, you must first import gimpfu. Then define the function that contains the code of the plugin. This is followed by a call to the register function. This registers the plugin, the parameters are mostly self-explanatory, more information can be found at http://www.gimp.org/docs/python/. At last the main function is called, which starts the plugin. This Hello World uses three different methods to output a simple message. A MessageBox with gtk, a Gimp function that prints a message at the bottom of the Gimp program and with stdout an output to the console.
Now start Gimp and go to Edit > Preferences > Folders Click on the plus icon to the right of Folder and go to Plugins. Here you can see the folders where you need to copy the plugin.
Copy the helloWorld.py into one of the specified folders and restart Gimp. The plugin is now available in the menu bar.
You are welcome to upload your programmed plugins to our website.