Wednesday, July 25, 2007

Tegan's Tech Talk - GIMP Scripting Part Three

This is the continuation of me learning how to batch script in GIMP. In the first part, I explained my quest. In the second part, I explained my script-fu. In this part, I explain how to run and batch my script-fu.

The quick way to test the script is to run it from the GIMP console (Xtns->Script-Fu->Script-Fu Console). To run it, I simply type script-fu-mylevels "TestIn.bmp" "TestOut.bmp" and hit the enter key and VIOLA! Ok, well, not quite... First I have to put the TestIn.bmp in the correct directory on my machine. In my case, I just put them in the same place as GIMP was running from, since that was easiest. I could put them elsewhere, put I need to put the whole path into the variable. For example: "C:\Documents and Settings\All Users\Documents\TestIn.bmp".

In the end, because it meant less typing, I decided to just stick it in the same directory that GIMP is running from (C:\Program Files\GIMP-2.0\bin). This is also how I ended up setting up my batch file, so I have to put files in that directory to run the batch on them. My excuse is that it was just easier. I'm sure I could figure out how to make it run in the folders I want it to run in, but I don't feel like taking the extra time to figure it out. Batch scripts are supposed to SAVE time.

Anyway, back on track, I use the GIMP console to test my script. If it works, I'm good. If it doesn't, I alter it. I actually ended up using this step quite a bit with the levels script, as I found numerous little problems with it, including that the channels was set to all red in the first version. Most of the other problems were typos on my part.

Once I knew the script would do what I wanted it to do, I had to batch it so I can run it on a lot of files all at once. In Windows, it is easy to create a DOS .bat file, so that's what I ended up doing. I learned DOS batching in college (not in classes, but from hanging out with comp-sci majors) and so I had a good grip on what I was doing in this section of the effort.

A DOS batch file is just a text file of DOS commands saved with the .bat extension. To run my script-fu, I used this:

for %%G in (*.bmp) do gimp-2.2 -i -f -d -b "(script-fu-mylevels \"%%G\" \"F%%G\")" "(gimp-quit 0)"
What this script does is take every file (%%G) that has an extension of .bmp and opens GIMP, runs my script on the file, saves the result with a prefix, then closes GIMP.

I ran the batch with a test group of 50 images, and it worked like a charm. At that point, I was satisfied. I managed, with a lot of help, to create a script and batch it in GIMP. If I need this again, maybe this write up will help me remember how it happened so I can duplicate the success.