batch scripting with GIMP on windows

I recently had the need to create a script to resize a large number of images (overwriting the original files).  I thought this should be straight forward using GIMP.  It was and it wasn't...

My nightmare started with learning lisp.  Lisp seems to be pretty straightforward once you get the hang of it.  In the few hours it took me to get the hang of it, my feathers got rather ruffled at having to keep track of so many parentheses and caddrdacadrar's.  Lisp wasn't the only tricky bit.  The GIMP documentation on how to register functions, and if you need to register them, and so on is so fragmented and confusing that I'm surprised anyone manages to write one of these damn script things at all.

Anyway, I worked it out, and here it is:

(define (batch-image-resize-width pattern width)
  (let* ((filelist (cadr (file-glob pattern 1))))
    (while (not (null? filelist))
      (let* ((filename (car filelist))
          (image (car (gimp-file-load RUN-NONINTERACTIVE filename filename)))
          (drawable (car (gimp-image-get-active-layer image))))
        (gimp-image-scale-full image width (/ (car (gimp-image-height image)) (/ (car (gimp-image-width image)) width)) INTERPOLATION-CUBIC)
        (file-jpeg-save RUN-NONINTERACTIVE image drawable filename filename 0.8 0 1 1 "" 0 0 0 1)
        (gimp-image-delete image)
      (set! filelist (cdr filelist))

    SF-STRING "pattern" "pattern"
    SF-VALUE  "width" "width"

The batch file I use to run this bad boy is:

"c:\Program Files\GIMP 2\bin\gimp-console-2.8.exe" -i -b "(batch-image-resize-height \"*.jpg\" 300)" -b "(gimp-quit 0)"

I should note that I am obviously running Windows here (GIMP 2.8), and apparently that some of this stuff is slightly different on Linux.

| June 24th, 2014 | Posted in Software |

Leave a Reply