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.




