I’ve been attempting to script Guile for the past two days, to do what I consider an unlikely task. My problem: I finally got some more CAT scan data for use in my research project, but Matlab can’t read the DICOM files. In fact, of several dedicated DICOM readers that I tried, only DicomWorks in windows could read all of them, and even loading them in there and exporting them back out didn’t solve the compatibility problem with Matlab. So, I realized that the Gimp can load and save DICOM files, and found that Matlab could read the output Gimp produced. I even found a way to get around the fact that Gimp erases all the patient’s tags.
Since I have a whole collection of these files, and I recalled Gimp was scriptable, I looked it up, and was pleasantly surprised to discover how easy it is to script Gimp using, of all languages, Scheme! I wrote up a simple modification of the sample script that just loads all the files in a dir and saves them back out as dicom files, and wrote a python script that applies the script to every subdirectory of my data directory. In ten minutes, I had solved my problem, or so I thought.
Here’s the problem: since I have so much data, and I was doing this on my personal computer, and have a separate repository on my math department account, I thought I could transport my script over to that account, and repeat the same process. That’s when I discovered how horrid and broken Gimp’s script-fu scripting system is. It turns out that the plugin I used to do the file globbing is not available in Gimp 2.0, which is the version the math department provides, so I figured that I could just write an equivalent in Scheme. Since I couldn’t find what scheme implementation Gimp was using, I assumed that it was Guile— one would think this is the obvious choice? But, as I discovered after my script kept crashing, Scheme actually uses the Scheme in One Defun implementation of scheme, which despite its glowing description:
SIOD is a small-footprint implementation of the Scheme programming language that is provided with some database, unix programming and cgi scripting extensions.
is in fact, almost amusingly broken. Here are some of the functions that are not available: getcwd, fread, fseek,fopen!, exec, exit, opendir, strcat. What’s up with that: it looks like each of these could be implemented in a couple of lines of C. Anyhow, since the file/dir handling of SIOD is so gutted, my script was doomed, and I had to refigure it.
To make a boring story shorter, I ended up using a python wrap around ImageMagick to convert the DICOM files to Tiff files— it turned out that besides being poorly scriptable and poorly documented, Gimp also reduces the number of gray levels in its DICOM output to 255, which makes them useless for anything but decorative purposes— and loaded those into Matlab.
Oh, here’s another annoying undocumented Gimp scripting quirk: each gimp function call, when specified at the command line, must be in a separate string, so
gimp -i -b '(gimp-scripting-sucks) (gimp-quit 0)'
will execute the first instruction, then hang, while
gimp -i -b '(gimp-scripting-is-weird)' '(gimp-quit 0)'
will execute the appropriate script, then exit as wanted.
Despite all this, I still admire the fact that you can command Gimp from the SIOD prompt by entering
gimp -i -b -. What would be excellent would be if Gimp switched/completed switching to Guile as the scripting engine. Then we’d have a real language as the backend, and the wonderful guile prompt as the front-end.