15 Jul 2011

batch rotate and image processing in GIMP

Wednesday Jun 15, 2011

The GIMP Script-Fu scheme snippet will take a file wildcard pattern ("*.JPG") and an rotate-type value (0 = 90° / 1 = 180° / 2 = 270°), level out the colors in the picture and save the changed image as a PNG file.

(define (batch-rotate pattern rotate-type)
  (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))))
             (set! filename (strbreakup filename "."))
             (set! filename (butlast filename))
             (set! filename (string-append (unbreakupstr filename ".") ".png"))
             (gimp-image-rotate image rotate-type)
             (gimp-levels-stretch drawable)
             (gimp-file-save RUN-NONINTERACTIVE
                             image drawable filename filename)
             (gimp-image-delete image))
           (set! filelist (cdr filelist)))))

The scheme function above can be stored into the users GIMP scripting directory (~/.gimp-2.6/scripts/) as "rotate-and-level.scm".

The shell script below can be used to start the batch rotate process on a directory of JPEG pictures.

# file
gimp -i -b "(batch-rotate \"${1}\" ${2})" -b '(gimp-quit 0)'

On a Unix shell, the glob file pattern needs to be quoted so that the shell will not expand it:

# ./ "*.JPG" 0
Other posts
Creative Commons License by Carsten Strotmann is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License .