If you're going to write your first GIMP plugin...

January 29, 2010
GIMP plugin

... then this post is for you. I used to utilize three scripting engines — Script-fu, PERL-fu and Python-fu, and now I wish to share my thoughts about their advantages and disadvantages.

It was started when I was asked to create a website with a special figured font applied on headings and product captions. If you're web designer then you know what my problem was — support of arbitrary fonts in different browsers is still up in the air. And that was a few years ago... In short, it was decided to make a bunch of images with captions and to put them on a page. In spite of a small size of the site, it was insufferably dull to make all these images by hand, do the same operations every time. So I started to look for automatic methods. I had no luck with ImageMagick, but it was not so hard for me to learn how to write GIMP plugins.

That was the first time I met Script-fu. And the first time I tried Scheme-like languages. Yes, syntax was a little bit tricky but this wasn't really important. I was impressed by a great library of plugins using this engine, many examples on the net, useful procedure browser that lets you construct a simple script in a few clicks.

Script-fu code snippet

(if (= transparent-bg 1) () (
  ; create layer: image, width, height, type, name, opacity, mode -> layer
  (set! back-layer (car (gimp-layer-new img (car (gimp-image-width img)) (car (gimp-image-height img)) RGBA-IMAGE "main" 100 NORMAL)))
  ; append layer to the image: image, layer, position
  (gimp-image-add-layer img back-layer 0)
  ; set background color: color
  (gimp-context-set-background background-color)
  ; fill the layer: drawable fill_type
  (gimp-edit-fill back-layer BACKGROUND-FILL)
  ; pull the layer down: image, layer
  (gimp-image-lower-layer-to-bottom img back-layer)
))

Script-fu is great when you need to write down some actions that you usually do with GIMP (by the way, it's strange that this image editor doesn't have an option for recording scripts as it was done in Adobe Photoshop or Microsoft Office — in my opinion, this thing is of high importance). Nevertheless, if you're going to do non-trivial operations, compute sizes, shifts, apply your own transformations, then prepare to face troubles with Script-fu. The second but not the least shortcoming is a poor debugging capability. After another GIMP release my script was broken and I can't figure out what happens in a suitable time. Finally, I stopped trying and decided to rewrite the script using another engine.

At the time I already tried PERL-fu for automatic processing of images from slide scanner. It's undoubtedly easier to write a code using a known programming language. You can pass variables to native PERL functions, output to file or stream any debugging info or regular messages for user. The great thing I like very much is ability to launch a special server that handles PERL-fu requests, so your plugin could look like a standalone image-processing application that uses all power of graphical editor. Unfortunately, I discovered that PERL-fu support was broken in GIMP-2.6 and there are rumors on the net that it's discontinued. Probably, it's concerned with increased popularity of Python-fu engine.

PERL-fu code snippet

# remove all layers that have different width or height than we expect
@layers = $image->get_layers ();
for ($i = 0; $i <= $#layers; $i++)
{
  if ($layers[$i]->width () != $width || $layers[$i]->height () != $height)
  {
    $image->remove_layer ($layers[$i]);
  }
}

Python-fu is much more like PERL-fu, the base of this engine is a `popular object oriented language Python. There is a special GIMP-Python console where one could try out some commands. Unlike Script-fu console there is no procedure browser here but all functions of the editor have the same interface so Script-fu calls could be easily converted to Python-fu calls. Also I was upset by a lack of Python-fu server option as PERL-fu has. This is a reason why batch processing with Python-fu (as well as with Script-fu) plugin is a mess. Each time you call plugin from a console it needs new instance of GIMP program. There are two alternatives here — first is to create internal cycle for creating or changing images, second is to put up with longer time of processing. You should decide by yourself. Maybe somebody will write such a server for Python-fu some day. Maybe it would be even me.

Python-fu code snippet

# create layer: image, width, height, type, name, opacity, mode
layer = gimp.Layer(image, "background", image.width, image.height, RGBA_IMAGE, background_opacity, NORMAL_MODE)
# append layer to the image
image.add_layer(layer, 0)
# fill the layer with color
layer.fill(BACKGROUND_FILL)
# pull the layer down
pdb.gimp_image_lower_layer_to_bottom(image, layer)

Resume. Script-fu is very useful for simple scripts but it would be harder to write something complicated and to debug a script. PERL-fu is broken but the idea with its server should be mentioned. Python-fu fits for any kind of scripts and allows you to use full power of Python language.

If you need a batch creating of images from text, you can download my plugin that was rewriten on Python-fu. I made a separate page for that. By the way, it's a good start for a beginner as modification of a simple working program is the best way to learn how to do such things!

Nikita Melnichenko.

Comments are closed