Script-Fu Template For Batch Processing In GIMP

Time and again, I’ve wanted to do a set of operations on a large set of images. I knew that Script-Fu in GIMP could do the job, but I could never find a boiler plate code to act on all files in a directory.

This is a little something I wrote to reduce the number of colors and remove the alpha channel of a large number of PNG files in a directory:

(define (smallify dir)
  (let*
    ((files-list (car (cdr (file-glob dir 0))))
     (do-one (lambda (f) (let*
                           ((i (gimp-file-load RUN-NONINTERACTIVE f f))
                            (d (gimp-image-flatten (car i))))
                           (begin
                             (gimp-posterize (car d) 5)
                             (file-png-save2 RUN-NONINTERACTIVE (car i) (car d) 
                              f f 0 9 0 0 0 0 0 0 0)
                             #t)))))
    (map do-one files-list)))

Here is the generic template that can be used for batch processing a lot of images in one directory:

(define (do-many dir)
  (let*
    ((files-list (car (cdr (file-glob dir 0))))
     (do-one (lambda (f) (your-code-goes-here))))
    (map do-one files-list)))

In order to use the above template, first replace (your-code-goes-here) with the s-expression you need to change the images. (Hint: Use Filters > Script-Fu > Console > Browse… to see the documentation). While doing so keep in mind that f will contain the full path to each file in your directory; you can use f to do what you wish that file.

Then in Script-Fu console, paste the resulting code in the text field there and press enter. This will define the do-many function. Then you can call it like this:

(do-many "/home/edwin/alotafimages/*.png")

And your s-expressions will act on all PNG files in that directory.

Advertisements

4 thoughts on “Script-Fu Template For Batch Processing In GIMP

  1. Oh, I just noticed that you’ve started using categories in your postings, is that some kind of new year resolution ? Anyways, thanks for this favour 🙂

  2. macports couldn’t install imagemagick for some reason, so I had to fall on GIMP. Can imagemagick do everything gimp can?

    abot the categories in my postings, it is just to make Christians find my stuff faster and get de-converted.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s