Saturday, January 2, 2010

Batch Resize Images in GIMP

It is always required to resize a set of images at once. So that, no need to do it one by one. Let's see, how this can be achieved using GIMP image editor in Ubuntu.

We are using the batch mode of GIMP to do image processing from the command line. Let's write a simple script in Scheme language to resize a set of image files.

(define (batch-resize pattern width height)
(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 height INTERPOLATION-CUBIC)
(gimp-file-save RUN-NONINTERACTIVE image drawable filename filename)
(gimp-image-delete image))
(set! filelist (cdr filelist)))))

This particular script takes a pattern for filename, desired width and height as inputs and resize all image files that are matched. This script overwrite the existing file. In order to run the script, save it with .scm extension in the ~/.gimp-/scripts/ directory.
Then goto the directory which contains images, then run the following command to resize images.

gimp -i -b '(batch-resize "*.JPG" 604 453)' -b '(gimp-quit 0)'
The above command will resize all the image files end with .JPG to 604X453
The above script can be customized to any other image processing requirements as well. Refer the Help -> Procedure Browser in GIMP for more operations.

The following script rename the resized image file by adding 'a' to the beginning of the file name

(define (batch-resize-rename pattern width height)
(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 height INTERPOLATION-CUBIC)
(let ((nfilename (string-append "a" filename)))
(gimp-file-save RUN-NONINTERACTIVE image drawable nfilename nfilename))
(gimp-image-delete image))
(set! filelist (cdr filelist)))))

By referring to the GIMP procedure and plugins browser and a Scheme tutorial, it is possible to customize the above script as required.

References: