Gimp: How To Write a Script-Fu Macro

Ranked #1,187 in Computers & Electronics, #18,784 overall

How to Write GIMP Macros to Speed up Your Photoediting

Gimp is a very powerful photoediting tool similar to Adobe Photoshop (except it is free) and like with Photoshop, macros or scripts can be written to increase productivity and to speed up repetitive tasks, like reducing the size of a lot of photos so you can email them, or adjusting the levels, contrast or saturation, but unlike some other programs the task of creating macros cannot be replaced or simplified with a macro-recording facility (i.e. perform the task once and record your actions, then repeat it just by "replaying" the macro)

GIMP does however have a "Script-Fu Procedure Browser" which aids the development of a Script-Fu macro. Script-Fu is a version of Scheme which is itself a version of Lisp.

This articles deals with how to create and use macros for this useful tool and how to use the Apple Mac command-line to run Gimp in batch mode (i.e. repeat a series of photoediting tasks on many photo files).

Script-Fu Procedure Browser

The Script-Fu Procedure Browser is a fairly intuitive browser that helps to find the function you want, perhaps corresponding to a GIMP menu function without having to resort to reading a manual. It also gives syntax and arguments for each function and a useful search feature.

To open the Script-Fu Procedure Browser, from the GIMP window use the following menu:

"GNU Manipulation Program": Filters -> Script-Fu -> Consule

(This may vary depending on the version of GIMP being used)

Once you know the syntax of the various commands you need you can put them together to form a function that performs the series of operations you require.

GIMP Photoediting

Loading

GIMP Books

Loading

GIMP from the Linux/Unix command-line: Mac OSX

Darwin Terminal Command-Line

Gimp commands can be used directly from the command-line.

Start a Terminal window either from X11 (X11 is Apples version of the X Server and must be installed in order to run GIMP see this article about installing GIMP if you don't have X11) and start gimp using the full path to the gimp executable file, which for an Apple Mac would probably be:

X11 Applications -> Terminal

OR

Macintosh HD -> Applications -> Utilities -> Terminal

(from Linux just open an xterm)

Start gimp in batch-mode either with the full path as below (or just gimp -b if you are using linux)

/Applications/Gimp.app/Contents/Resources/bin/gimp -b -

Then type the Script-Fu commands at the gimp prompt e.g.

> (gimp-image-new 256 256 RGB)
(1)
>(gimp-display-new 1)
(1)

The (1) is the image-id for the new-image, returned by the first function and may not be (1) and is the argument in the second command.

Computer Articles

Loading

Defining a function

Now you have established the commands you want to use they can be combined into a new function inside a let* statement. e.g. here is an example of creating a new image, passing the image-id to the variable img-id, creating a new layer (lay-id) filling that layer, then displaying the image created:

(define (create-simple-image)
(let* ((img-id (car (gimp-image-new 512 512 RGB)))
(lay-id (car (gimp-layer-new img-id 512 512 RGB-IMAGE "layer-name" 100 NORMAL-MODE))))
(gimp-drawable-fill lay-id BG-IMAGE-FILL)
(gimp-image-add-layer img-id lay-id 0)
(gimp-display-new img-id)
img-id))

the image-id ("img-id") is then returned after the function is run as shown below:

> (create-simple-image)
7

car is the first part of a list, cdr the rest of the list and cadr is the first part of the rest of the list (the same as in the languages Lisp and Skill)

O.K. That's all very well, but not very useful for manipulating photographs. If you have scanned photos from a digital scanner or photos in a digital camera you may want to process them for use on a web-page or emails etc.

Here is an example of function that will read a jpeg file, adjust the levels (gimp-level-stretch automatically adjusts the levels histogram) and change the size of the photo (i.e. change the number of pixels) then writes out a new jpeg file:

(define (photo-shrink width height input output)
(let* ((image (car (file-jpeg-load 1 input output)))
(drawable (car (gimp-image-active-drawable image))))
(gimp-levels-stretch drawable)
(gimp-image-scale image width height)
(file-jpeg-save 1 image drawable output output 0.8 0 1 1 "" 0 1 0 0))
)

alternatively the function (file-jpeg-load ... ) could have been used in the same way to read a TIFF file from a scanner (and other functions exist for other file formats)

This can then be called from the Gimp command-line like this:

> (photo-shrink 100 100 "IMG_0665.JPG" "IMG_0665-z.JPG")

which will read the file "IMG_0665.JPG", fix the exposure and shrink it to 100 x 100 pixels and output the file "IMG_0665-z.JPG"

Scheme Programming Language

GIMP Script-Fu

Loading

Simple "Batch" Process

So now we have a function to manipulate a photo file, but how do we use it to manipulate lots of files all at once?

The quickest way (if not the most elegant way) to do this is just to paste the function into Gimp for each file in a directory. i.e. list the files in the directory, dump the file-names to a file and build a script, then paste it into the Gimp command-line.

e.g. from a Terminal window in the appropriate directory

> ls > simple-script

then edit the file simple-script to define the function photo-script function then call the photo-shrink function for each jpeg file then start gimp and paste the file:

/Applications/Gimp.app/Contents/Resources/bin/gimp -b -

Welcome to TinyScheme, Version 1.38
Copyright (c) Dimitrios Souflis

> (define (photo-shrink width height input output)
(let* ((image (car (file-jpeg-load 1 input output)))
(drawable (car (gimp-image-active-drawable image))))
(plug-in-autocrop 1 image drawable)
(gimp-levels-stretch drawable)
(gimp-image-scale image width height)
(file-jpeg-save 1 image drawable output output 0.8 0 1 1 "" 0 1 0 0))
)

(photo-shrink 100 100 "IMG_0665.JPG" "IMG_0665-z.JPG")
(photo-shrink 100 100 "IMG_0668.JPG" "IMG_0668-z.JPG")
(photo-shrink 100 100 "IMG_0667.JPG" "IMG_0667-z.JPG")
(photo-shrink 100 100 "IMG_0666.JPG" "IMG_0666-z.JPG")
(photo-shrink 100 100 "IMG_0671.JPG" "IMG_0671-z.JPG")
(photo-shrink 100 100 "IMG_0670.JPG" "IMG_0670-z.JPG")
(photo-shrink 100 100 "IMG_0669.JPG" "IMG_0669-z.JPG")
(photo-shrink 100 100 "IMG_0672.JPG" "IMG_0672-z.JPG")

This will have fixed the exposure (i.e. run auto-levels) for every file listed, auto-cropped any unused pixels round the picture, resized to 100 x 100 pixels and output a jpeg. This could easily be altered to read a TIFF file (or any other format) from a digital scanner, auto-crop the blank edges (function plug-in-autocrop or plug-in-zealouscrop) fix the levels, resize and convert to JPEG format.

This method works in an Apple Mac Terminal window, but if you are editing a lot of large files could result in memory issues, so another way to run Gimp from the Apple Terminal command line is to feed the function definition and opperations into gimp from a file:

> /Applications/Gimp.app/Contents/Resources/bin/gimp -b - < file.txt

where file.txt contains the function definition above and one or more function-calls, followed by the function (gimp-quit 0) to return control to unix. This can then be repeated for each file name in the directory although a new file.txt for each filename, or some copying/moving of files between each gimp run (see the next section)

This is a bit of a hack, but if you want to do this properly you will need to use the script-fu-register function to register the function and script-fu-menu-register if you want to add it as a menu item and save the scripts in the default scripts directory so that gimp can pick them up from a proper batch run (see below)

Featured Lensmaster

Registering a Function

After writing a function it can then be registered to allow it's use from a GIMP menu, using the script-fu-register function. Alternatively after defining the function on the GIMP command line it can be used repeatedly from the command line or as a batch process (See below)

(script-fu-register
"macro-name" ; Function Name
"macro-name" ; Menu Label
"Do something to a photo..."
"Andy" ; author
"Andy" ; copyright
"2009-11-19" ; date created
"" ; image type that script works on

; Any Other Variables that need to be defined... (need to check documentation for the these options)

)

The scripts should be saved to a text file in directory ~/.gimp-2.6/scripts/ e.g. create-simple-image.scm then run

Script-Fu -> Refresh Scripts

to load the scripts in the scripts directory.

Running Gimp in Batch Mode

From the command Line

to invoke Gimp from unix or linux either use command line or a shell script similar to the following:

gimp --display :1.0 -c -i -d -b '(macro-name arg1 arg2 100 "file1" "file2")' '(gimp-quit 0)'

where macro-name is the name of an already defined and registered function that takes two file-names as input (file1 and file2) and two other arguments (arg1 and arg2); reads file1 performs some action on that file and saves to a new file.

Simple shell script for batch Gimp to run a macro on every file in a directory:

#!/bin/sh
for file in *; do
echo Reading: $file;
/Applications/Gimp.app/Contents/Resources/bin/gimp --display :1.0 -c -i -d -b '(macro-name Arg1 Arg2 "'$file'" "'$file'2")' '(gimp-quit 0)'
done

This didn't work from the Apple Mac; for a start for Apple Mac terminal Window ("Darwin" version of unix) the full path to gimp and an extra hyphen at the end will be required instead of just gimp: e.g. /Applications/Gimp.app/Contents/Resources/bin/gimp -c -i -d -b -

but I found the following did work for the Apple Mac (and the function doesn't need to be registered because it is defined each time it is called):

create a text file gimp-batch-script2:

(define (gimp-photo-fix input)
(let* ( (output (string-append input ".jpg")) (image (car (file-tiff-load 1 input out
put)))
(output (string-append input ".jpg"))
(drawable (car (gimp-image-active-drawable image))))
(plug-in-autocrop 1 image drawable)
(gimp-levels-stretch drawable)
(file-jpeg-save 1 image drawable output output 0.8 0 1 1 "" 0 1 0 0)))
(gimp-photo-fix "temp")
(gimp-quit 0)

and bin file gimp-batch-script:

#!/bin/sh
for file in *; do
echo Working on file: $file;
cp $file temp;
/Applications/Gimp.app/Contents/Resources/bin/gimp -b - < ./gimp-batch-script2;
mv temp.jpg $file.jpg
done

> chmod 775 ./gimp-batch-script

then run it:

> ./gimp-batch-script

That will copy each TIFF file in turn to temp, start gimp and run the script reading temp and writing temp.jpg then copy temp.jpg to the original filename (with a .jpg appended). O.K. not elegant, but it works

Adobe Photoshop

Loading

All About Me

Loading

My Zazzle Gallery

Loading

Please Leave Some Feedback

  • Tipi Apr 2, 2012 @ 6:48 pm | delete
    Returning with a little angel dust for this Gimp help excellence!
  • winlin Jan 14, 2012 @ 8:01 am | delete
    I've used GIMP, but have yet to utilize script, Thanks for the tutorial.
  • SteveDOF Dec 27, 2011 @ 10:07 am | delete
    Gimp is an impressive endeavour, it looks extremely useful and highly polished, but as an illustrator, the one thing that stops me dead, even before downloading it, is the lack of a built-in macro recorder.

    I don't care how easy and "fun" script writing is. It isn't something I am even vaguely interested in. If you enjoy it fine, I wish you well, but I draw and paint, that's my job, it's my past-time, that is what I want to use a paint program for, not to write scripts.

    I'm sure there are others like me, how many is impossible to say, but the time and yes, money, that a good in-built macro recorder saves is enormous. As far as I can see from watching many videos and reading about Gimp, Photoshop's actions recorder remain it's single biggest tool advantage over Gimp.

    Give me a built-in macro recorder that rivals Pshops actions and I'd download gimp tomorrow, but until then, it would be pointless, from my point of view, to even try the program out.
  • AndyPo Jan 25, 2012 @ 5:34 pm | delete
    Good point. I can create a macro in no time and I enjoy it, but a macro recorder is the easiest way to do it if you don't like script-writing.
  • mojweb Nov 28, 2011 @ 4:12 pm | delete
    Great tutorial. Thanks.
  • BaseCandy Nov 8, 2011 @ 4:11 pm | delete
    thanks a lot
  • fiftysquid Oct 27, 2011 @ 11:44 am | delete
    Thanks Andy, I'm going to bookmark this lens for future reference! Very informative, thanks!
  • cgreen7090 Sep 12, 2011 @ 10:57 pm | delete
    wow, what a lot of hard work. Thanks.
  • tstandiford Sep 4, 2011 @ 10:59 am | delete
    Awesome! I've looked everywhere for something like this.

    Thanks so much for sharing this.
  • gypsyman27 Jul 10, 2011 @ 2:58 am | delete
    Andy, this lens is a saving grace for me. I've been running Gimp on my laptops and my son's desktop, but they are PCs no Macs. Now I have this Mac, because they're supposed to be better for artists, but I'm having a lot of trouble installing the Mac version of Gimp. Now I see where I wandered off the path and I realize what I need to do, so I'm bookmarking this page for help in the future. Thank very large for sharing this information. If you're in the states stop by my place in Virginia and I'll try and make us even.:~) I note that Gimp is written in a language similar to LISP. We used to use a form of LISP in one of our CAD packages. It was AutoCAD, and AutoLISP, to make a long story short (too late) we used to call it Lost In Stupid Parenthesis. I know it's corny, thanks again. See you around the galaxy...
  • AndyPo Jul 10, 2011 @ 11:54 am | delete
    Thanks very much. I'm glad this was useful. LISP was used as the basis for quite a few CAD packages, I think. I used it in the form of "Skill" for many years, which was used in some silicon chip design industry CAD packages, so I was quite pleased to find out that is was the basis of Gimp too.

    Look forward to seeing you next time I'm in Virginia ;-)
  • AndyPo Jul 10, 2011 @ 12:05 pm | delete
    I forgot to say, we also had a similar joke: "LISP stands for Lots of Irritating Superfluous Parentheses"
  • sukkran Apr 11, 2011 @ 1:22 pm | delete
    thank you very much for the useful info about photo editing with gimp.
  • ElizabethJeanAllen Mar 8, 2011 @ 4:58 pm | delete
    Great info!
  • ElizabethJeanAllen Mar 8, 2011 @ 4:58 pm | delete
    Great info!
  • valzart Mar 2, 2011 @ 11:57 am | delete
    Great Photography sweetheART & good news about gimp ;~} Valz
  • Philippians468 Jan 23, 2011 @ 10:36 am | delete
    im really impressed with your knowledge! cheers
  • Tipi Dec 18, 2010 @ 9:13 pm | delete
    YOu sure know what you're doing, my friend!
  • Michey Dec 4, 2010 @ 4:59 pm | delete
    Excellent learning to mastermind Gimp, I came back to bless this lens. Always from your lens I learn something, and this one is no exception.
    Regards
  • cbessa Dec 2, 2010 @ 9:30 am | delete
    Thanks for the tips. I am starting to use GIMP as a PS substitute and will be pretty useful, because i am still kind of shaky with Gimp interface and ways.
  • Margo_Arrowsmith Dec 1, 2010 @ 11:25 am | delete
    Great reference, thanks for the work and information
  • Michey Nov 18, 2010 @ 6:22 pm | delete
    I download Gimp, but I didn't get over the learning curve. I am still using Corel Photo-Paint... just because I use to it. I am not lazy, and I like to learn new thinks, but my time is very limited, so I have t relay in short cuts and do my nest.
    Great lens Andy.
  • OhMe Sep 11, 2010 @ 7:34 am | delete
    I've been wanting to try Gimp but haven't yet. Most of this is way over my head but I did appreciate learning a little more about Gimp. One of these days I will get up the nerve to try it. I like my old Picture It program and so far it is working fine for me. Blessed.
  • Sep 9, 2010 @ 5:56 pm | delete
    Thank you for the tip....free is good.
  • Norma_Budden May 22, 2010 @ 11:30 pm | delete
    Well, I'm in the process of updating my Purple Star lenses so will definitely drop back here again (since it's growing late) tonight. Congratulations on pulling another Purple Star in and thanks for letting me know earlier. I've just gotten around to featuring it now...my apologies for the delay.
  • hlkljgk May 21, 2010 @ 8:27 pm | delete
    gimp is a great tool; thanks for the info - congrats on your purple star :)
  • ElizabethJeanAllen May 21, 2010 @ 5:36 pm | delete
    I bought a new camera this spring. I've been using Photoshop to edit them but I need to check this out. Photoshop and I are not on the best of terms.
    Thanks for sharing
    Lizzy
  • JaguarJulie May 6, 2010 @ 10:42 am | delete
    Wow! This is hugely interesting ... and then I started thinking outside the box, as to what macros would help lensmasters in their publishing / updating of a portfolio of lenses. Do you supposed somebody already has written that macro for automating that process? I thought I heard or read something about just that. Very interesting Andy!
  • AndyPo May 10, 2010 @ 9:00 am | delete
    That's an interesting thought. It wouldn't be possible to use this macro language to auto generate or update lenses though. It only works on image files. I suspect Squidoo wouldn't want the lenses auto generated anyway as some people might use it to generate lots of "Spam" lenses. An auto update would be useful though.
  • JaguarJulie Sep 8, 2010 @ 3:00 pm | delete
    Ah ... this still fascinates me! Go ahead, say Script-Fu Macro fast three times!
  • Apr 1, 2010 @ 2:04 pm | delete
    This lens is awesome. I love it. I am going to tell my editors on my Thai News website to write something about this lens and probably feature it.

    I will comment here again once we do.

    Great Work
  • a_willow Nov 26, 2009 @ 11:03 am | delete
    Great tutorial! :) Well done!
  • Sylvestermouse Nov 20, 2009 @ 8:51 pm | delete
    I have asked for Photoshop for Christmas, so I will be back for the additional help! Thanks!
  • Jewelsofawe Nov 20, 2009 @ 7:47 pm | delete
    Over my head... I just use photobucket so I don't have to figure it out too much.
  • Laniann Nov 20, 2009 @ 9:40 am | delete
    Beautiful photographs, as always. I can see how this would save a lot of time. Good clear instructions.
  • rms Nov 20, 2009 @ 6:23 am | delete
    Great tutorial!

Some Related Articles

Loading

by

AndyPo

I live with my my wife and son in London, England, but have worked and travelled all over the world. I am a semi-professional wildlife and travel photographer... more »

Feeling creative? Create a Lens!

Scheme Programming Language 

The Language of The GIMP

The Scheme Programming Language

Amazon Price: $29.94 (as of 05/31/2012)Buy Now

Scheme is the programming language used by GIMP for the macro-driven and batch processing techniques discussed in this article. It is similar to LISP and is a simple, but powerful language useful for quickly creating scripts to manipulate your photographs and images. Once you have manipulated one photo you can turn the actions you took into a script written in Scheme and run the process on all of your images.

GIMP Books 

Beginning GIMP: From Novice to Professional

Amazon Price: $27.47 (as of 05/31/2012)Buy Now

Learn how to use GIMP from simple image fixes to complex layer-manipulation and other professional effects.

Gimp Photo Editing: The Basics 

Loading