English Language

Scripting in Gimp. The hard and the nice way…

Small problem intro…

I am writing another book. I want to add numbered bullets to a screenshot. All bullets added as images each on a separate layer to easy the moving around. This way I can reference these bullets from the text in my book. Well, why not do some scripting in Gimp to make this happen?

It all sounds harmless enough now, doesn’t it?.

First: the hard way…

I thought: “Well, the script-fu scripting language, as is in use in the excellent GIMP program, has been around for a long time. There should be enough example scripts and I will be out of the woods in minutes.”. Well, you can just forget about that.

It is like the Lisp programming language so you will be writing a lot of “(” and “)”.

To find out more about defining the function and registering it with the Gimp program I direct you to other webpages. There is enough info on that in the script-fu tutorial.

Anyway, at the end of a hard days work, I had a script that worked! It added a layer to an image, it loaded a small image and placed it on the new layer and also gave the new layer a name.

It looks like this (beware of the newlines) :

  "script-fu-jb-count"                        ;func name
  "JB count"                                  ;menu label
  "add layers with numbers."                  ;description
  "Jeroen Baten"                              ;author
  "copyright 2013, Jeroen Baten"              ;copyright notice
  "October 10 2013"                           ;date created
  ""                                          ;image type that the script works on
  SF-IMAGE    "Image"         0

(script-fu-menu-register "script-fu-jb-count" "/Scripts/JB-Scripts")

(define (script-fu-jb-count theImage )
    (new-layer1 nil)

    ; Create a new layer
    (set!  new-layer1  (car (gimp-file-load-layer 1 theImage "piclib/1.png" ) ) )
    ; Give it a name
    (gimp-layer-set-name new-layer1 "Laag 1")
    ; Add the new layer to the image
    (gimp-image-add-layer theImage new-layer1 0)

Sofar, so good. Turns out there is (almost) no looping possible in script-fu. Well, there is, but you really don’t want to go there (just sayin’).

Second: the nice way:

So I turned to using the Python-fu stuff (I love python). If you compare the two scripts the differences should be easy to spot:

#!/usr/bin/env python

from gimpfu import *

def insert_numbers(theImage,count) :
    new_layer1 = pdb.gimp_file_load_layer(theImage, "piclib/1.png")
    pdb.gimp_layer_set_name(new_layer1,"Laag 1")

    "Insert numbers",
    "Add layers to an image with counters in them",
    "Jeroen Baten",
    "Jeroen Baten",
        (PF_IMAGE, "img", "Image:", None),
        (PF_STRING, "string", "Aantal", '1'),
    insert_numbers, menu="/JBCount2")


In short:

  • In all internal Gimp routine names the “-” is replaced with an “_”.
  • A “pdb.” is placed before all internal Gimp routine names.

Now, adding looping to this should be a walk in the park.


Beware if the fact that Script-fu stuff belongs in “~/gimp-2.8/scripts”  and Python-fu scripts belongs in “~/gimp-2.8/plug-ins”.