September 21, 2014
 
 
RSSRSS feed

Creating a Fancy 3D-Effect GIMP Plugin in Python - page 2

Your Fresh New BlobiPy Plugin

  • April 23, 2009
  • By Akkana Peck

The GIMP Procedure Browser

You have the skeleton of a script; you know the steps you want to implement. How do you turn those steps into a Python program?

Fortunately, GIMP gives you a way of searching for functions: the Procedure Browser, available in GIMP 2.6's Help menu. In GIMP 2.4 and earlier it was in the Xtns menu in the Toolbox window. Type a pattern into the search field at the top and GIMP will show you all the matching functions in its Procedural DataBase, or PDB.

The first step in the blobify procedure is "Alpha to Selection". If you're comfortable with regular expressions, you can search for patterns like alpha.*selection and selection.*alpha. Otherwise, just try each word separately and choose the one with the shortest list -- in this case, "alpha".

There are 28 procedures with "alpha" in the name. That's a lot. But look through the list for something that also has "selection" in the name and you'll find only one: gimp-selection-layer-alpha.

gimp-selection-layer-alpha takes one parameter: the layer to work on. In the python_blobipy function, we know the layer, because it was passed in along with the image. You can call PDB functions from gimp-python like this:

    pdb.gimp_selection_layer_alpha(layer)

I've replaced the dashes GIMP shows in the Procedure Browser with underscores, since Python uses dashes for subtraction.

Now repeat the procedure for the other steps in the process. After Alpha to Selection, the next step is Select->Invert, so look for invert in the Procedure browser and you'll find gimp-invert. It takes one parameter: the drawable to work on.

Next is the drop shadow. You'll find script-fu-drop-shadow easily enough, but what are all those parameters?

Fear not: parameters for PDB functions are almost always the same as the ones you see in GIMP's user interface, like the drop shadow dialog.

The run-mode is something that's handled automatically from gimp: it ensures that drop shadow won't show its dialog when it's being called from another script. image and drawable are your img and layer. Blur radius is the Blur parameter you're letting the user choose. For this example I'll set Offset X and Y to be -3, but you might want to let the user set them, or calculated it from the blur value. I'll keep Opacity at 80% (the default for Drop shadow), and of course Allow resizing needs to be turned off (False) since that was one of the blobify steps. So that gives:

    pdb.script_fu_drop_shadow(img, layer, -3, -3, blur,
                              (0, 0, 0), 80.0, False)

The final step is to clear the selection -- that's gimp_selection_none. So this is what those basic blobify steps look like in Python:

def python_blobipy(img, layer, blur) :
    pdb.gimp_undo_push_group_start(img)

    # Do stuff here
    pdb.gimp_selection_layer_alpha(layer)
    pdb.gimp_selection_invert(img)
    pdb.script_fu_drop_shadow(img, layer, -3, -3, blur,
                              (0, 0, 0), 80.0, False)
    pdb.gimp_selection_none(img)

    pdb.gimp_undo_push_group_end(img)

Undo

Wait -- what are those first and last lines?

gimp_undo_push_group_start and gimp_undo_push_group_end tell GIMP to group operations together, so that if the user runs Undo, GIMP will undo the whole blobify operation in one step. They're always a good idea if you're doing more than one or two steps.

And there you go! Your own GIMP Python plug-in, and it even does something useful. You can use the Procedure Browser to figure out how to turn any simple set of GIMP steps into a plug-in.

References:

Official GIMP-Python documentation (unfortunately somewhat out of date)

Akkana Peck is a freelance programmer whose credits include a tour as a Mozilla developer. She's also the author of Beginning GIMP: From Novice to Professional.

Sitemap | Contact Us