| Batch bitching This small tutorial shows you how to work on an image in batch mode using Gimp. The loaded image gets rescaled and saved again. This is usually performed on web servers performing dynamic image processing and similar tasks. Don't expect the ultimate guide on batched image processing, this document is just a start aid. :-) Comments are welcome! |
| Step 0: Getting started First of all, you'll need a copy of Gimp, I used 1.2.1 and suggest eveyone to update to, at least, 1.2.0 because of possible API changes. Further, you'll need a running X server or a Virtual Frame Buffer. The latter ist useful if you don't need any graphical output or haven't got a X server. |
| Step 1: Invoking Xvfb If no X servers are running on your machine then try Xvfb :0 -screen 0 10x10x8, with one X server running, try Xvfb :1 -screen 0 10x10x8. This enables a screen size of 10*10 pixels with a color depth of 8 bit. Larger screens require more memory and aren't really useful. |
| Step 2: Scripting Now, we need to write a script containing the operations done to our image. I won't discuss programming scripts here but post a simple ready script. (define (script-fu-rsc width height infile outfile) (let* ((img (car (file-jpeg-load 1 infile infile))) (drawable (car (gimp-image-active-drawable img)))) (gimp-image-scale img width height) (file-jpeg-save 1 img drawable outfile outfile 0.8 0 1 1 "No comment!" 0 1 0 0))) (script-fu-register "script-fu-rsc" "<Toolbox>/Xtns/Script-Fu/Tools/RSC" "RSC" "Michael Spunt" "Michael Spunt" "2001-04-24" "" SF-VALUE "Width" "100" SF-VALUE "Height" "100" SF-FILENAME "Infile" "infile.jpg" SF-FILENAME "Outfile" "outfile.jpg")Check out titix' great tutorial on writing Script-fu if you're not familiar with it yet. |
| Step 3: Script location Copy your script into Gimp's script-fu direction. Usually, it's /usr/share/gimp/1.2/scripts/ (global) or ~/.gimp-1.2/scripts/ (personal). |
| Step 4: Calling the Gimp manually Now, it's time to invoke the Gimp. This can be done either via command line or a shell script you've made. This is the basic sctructure: gimp --display :1.0 -c -i -d -b '(script-fu-rsc 100 100 "input.jpg" "output.jpg")' '(gimp-quit 0)'. --display :1.0 means, Gimp should connect to screen 0 on display 1 (change this option to fit your Xvfb settings). -c -i -d make Gimp print messages to console instead of dialog windows, don't display a user interface and skip loading brushes and other additional data. Type man gimp to get more detailed explanations on those options. -b, finally, lets Gimp execute the batch commands enclosed in apostrophes, ie our script and exit command. The image input.jpg gets rescaled to 100 100 and is saved as output.jpg That's all. Now you can write a simple shell script which runs Gimp on all images inside a directory. |
| Step 5: Calling the Gimp automatically This is a simple shell script which demonstrates a possible batch usage of Gimp: #!/bin/sh for file in *; do echo Working on file: $file; gimp --display :1.0 -c -i -d -b\ '(script-fu-rsc 100 100 "'$file'" "2'$file'")'\ '(gimp-quit 0)' doneThis opens every file in the directory and applies the operation as described above to it. Be careful with spacings when typing/copying this! |