Saturday, 25 April 2009

Batch Image Editing using Script-Fu & GIMP

I have a large number of bitmap images which i need to include in a latex document. I didn't want to manually scale the images and save them to PNG format so I looked for ways of automating GIPM. Script-Fu is the language/interface for GIMP. It is unlike any other language I have used before and so it took me a little time to work out what was going on.

The script below, when saved to ~/.gimp-2.6/scripts/ directory can be used to convert any image file to a PNG and scaling it in the process to 150px by 150px.

(define (chris infile outfile)
(let* ((image (car (gimp-file-load RUN-NONINTERACTIVE infile infile)))
(drawable (car (gimp-image-get-active-layer image))))
(gimp-image-scale image 150 150)
(file-png-save-defaults 0 image drawable outfile outfile)

To run it from the terminal:

gimp -i -b '(chris "infile.bmp" "outfile.png")' -b '(gimp-quit 0)'


Or to run this automatically on all bitmap files within a directory use this bash script:

#!/usr/bin/env bash

for myfile in *.bmp
echo Converting $myfile...
gimp -i -b "(chris \"$myfile\" \"$myfile.png\")" -b "(gimp-quit 0)"




Stephen English said...

Some impressive Lisp skillz there. Any reason you went for Gimp over Imagemagick?

Something like:
for x in *.bmp; do convert $x -resize 150 `basename $x`.png; done


Chris said...

Hmm, yeah, I was so used to using GIMP for this sort of stuff that, regrettably, I didn't look as far as Imagemagick. At least i learned a little Lisp though - although i'll try to avoid ever having to use it again :O its horrible.

Smiler said...

Those who can program LISP well praise it for being one of the most elegant programming languages around. Can't say that's my opinion though. I'd always wanted to try it: functional programming FTW!

Anyway: isn't 150x150px a bit small for a LaTeX image? If you're going to print that at 300DPI (which most laser printers do), then the image will only be 0.5x0.5 inches. What you ought to do is increase the 'resolution' of the PNG (tricky to do, but it's in the file somewhere) to 300DPI. Alternatively, graphicx can handle this: just include scaling dimensions when you include the image:

If you don't do this, then you'll get a pixelated image (unless that's what you wanted, of course).

Chris said...

you still can't use bitmaps in latex documents, so i have to convert them to pngs. Also the report is already at 80 pages and takes an age to compile each time. By scaling the images using gimp/imagemagick it speeds things up a little bit.

batch image converter said...

Batch editing means collective editing of a collection. If you want to edit a batch of photographs all together then it’s a good option to use a batch editing software for this purpose.

Marek Cajkovsky said...

This prompts me to save the destination file, can it be done automatically ?

Ben said...

Script Fu and GIMP is great image editing software I've been using since a long while and truly very much pleased with these. So thanks dude :)

Michael Fuller said...

Impressive, Photo editting is my hobby, I often make stock photos using the Apps from AppStore. Want to share with you one great website where you can get photo editing software for mac sure you find there something useful for you. Good luck.