Photo Tutorial: The basics of ImageMagick

Dan Lyke

Member
Joined
Jan 22, 2007
Messages
321
What's this? An image editing tutorial with no pictures?

If you're an old school computer hacker like me, you're probably much happier at a command line than you are with a mouse in your hand. Or maybe you've got a hundred images, all of which need to be cropped, resized, and saturation boosted, and the prospect of doing this with the mouse makes your wrists hurt, it's time to turn to ImageMagick, an Open Source, free suite of batch mode image manipulation tools.

However, don't go here unless you're comfortable with a command line interface. And several of the things I'll demonstrate here use a Unix shell called "bash" which is usually the default command line on the Mac or Linux computers, but is only available via third party applications on Windows. I end up installing Cygwin on all of my Windows computers because it would be charitable to describe the Microsoft command line tools as "not state of the art", but if you're familiar with them you can probably figure out your own way to do these operations.

There are several utilities which ship with ImageMagick, the three that I find most useful are "convert" and "mogrify". Convert takes one image, applies a bunch of changes to it, and writes it into another image. Mogrify reads one image, does the operations, and then writes it back to the same image.

For instance, in creating Photo Tutorial: Downsizing Images in GIMP, I had a number of files I wanted to convert from TIFF format to GIF format, so in the Mac's command line I typed:

 

Code:
for a in file1 file2 file3 file4 ;

 

Code:
do convert $a.tiff $a.gif ;

 

Code:
done

And it created .gif versions of  file1.tiff through file4.tiff. Real command line junkies could have done this without specifying the filenames, by just saying:

 

Code:
for a in *.tiff ;

but the process of stripping the extension off of $a would make the example harder to read.

A more prosaic operation is that for every file I download off of my camera I make a smaller version for thumbnails and to put on my web site. That looks something like:

 

Code:
convert HPIM0450.JPG -size 640x480 -scale '640x480>!' -normalize HPIM0450_thumbnail.jpg

The "-size" option says "the output is going to be small, so you can take shortcuts on the way because we don't need to keep the full quality", the "-scale" option says "we're scaling the image", and the ">!" silliness after that says "really, we don't care that we may be distorting the aspect ratio", and the "-normalize" says "if this was taken in low contrast light, up the contrast so the image looks crisper".

One way to do this to an entire directory of images might be to copy all of the files to a new directory and then use "mogrify" to rework those files in place. The following command would resize all of the .jpg files in a directory and give them a Disneyland like color saturation:

 

Code:
mogrify -size 640x480 -scale '640x480>!' -modulate 100,120,100 *.jpg

You can do almost anything with the command line options, and there are plenty of cookbooks out there, especially in the digital photography forums, for standard operations (like adding a copyright notice and a border to all of your images) but this is not a set of tools for everyone, so don't feel too bad if none of this makes any sense at all to you.
 
Back
Top