Search

Work the Shell - Messing around with ImageMagick

I've written previously about working with graphic images within shell
scripts, and obviously, it's a little bit tricky because, well, scripts
generally are strongest working with text, and you can't even see
graphics, let alone manipulate them directly. Further, let's be
candid, the suite of utilities included with a stock Linux/UNIX system
doesn't include much that help you work with graphics or image files at
all.

Fortunately, there's a splendid open-source package called ImageMagick,
which actually is designed to make working with image files from
the command line easy and fast. It's the smart back end to a bunch of
image utilities, and with a quick trip to
www.imagemagick.org, you can download it too.

A couple different steps are involved in installing it, and this
time, I'm actually going to play with my Apple MacBook Pro and install
the utilities to live within the Darwin world of Mac OS X.

Installing ImageMagick in Darwin/NetBSD

Since 99% of the time that I'm using my Mac I am logged in as
taylor, I'm going to opt to drop the software into my own
personal bin directory rather than the more standard location of
/usr/local/src (with the binary in /usr/local/bin). It might be
that I'm a long-term UNIX geek or something, but I have my own ~/bin
(or $HOME/bin, if you prefer) directory anyway, so once the binary file was
downloaded, here's what I did:

cd ../bin
tar xvf ../Downloads/ImageMagick-i386-apple-darwin9.6.0.tar

Because this particular distro includes precompiled binaries, it's as
easy as just tweaking a few environment variables to add the unpack
directory and proceed:

These are best added to your ~/.profile or ~/.cshrc (if you're using
Csh, but why would you?), so that they're invoked each and every time
you log in or, in the case of the Mac environment, spawn a new Terminal
shell.

It's a good idea to test the newly installed programs too. Find a .gif,
.jpg or .png file and see what the ImageMagick identify program
says.
Here's how I did that:

Where identify really shines is with JPEG files, which the
file command can't quite seem to figure out. Why that's
true, I don't know, but that shortcoming is the main reason I have ImageMagick
installed on my system.

Doing Something Useful with ImageMagick

One of my hobbies is photography, and as a parent, I find that I frequently
end up as the “official” photographer for school events. I recently
did just that for my daughter's May Fair event, and I ended up with about
500 5–8MB image files that were great for printing (about 4,200x2,800) but
not so good for viewing on the computer screen. What I wanted to do was
create images that were approximately 1,024x800 or thereabouts, so that
they'd view at 100% on a typical computer screen, in a directory that
paralleled the original image file directory. That way, parents could view a
slideshow of the smaller images and then grab the identically named big
image if they wanted to upload it and order prints.

With ImageMagick, this is easy. In fact, if I wanted to use the
mogrify command, I could have very easily done everything in a
single command, but because I like obscure, complicated solutions rather than
simple, elegant ones, I decided to use the convert command
instead.

The challenge is that, like everything else in ImageMagick, the convert
app has a staggering number of different command flags. Type
convert, and
you'll see what I mean.

Digging through them, here's the flag I want to use:

-resize geometry resize the image

That sounds like what we need is to resize the images, though
“geometry” is
still a bit of an unknown. Now it's time to pop over to the ImageMagick Web
site,
where we find a ton of options for geometry, including:

Once you've gone through the hassle of installing the ImageMagick
program, it's delightful to see how easily many different tasks can be
accomplished.

Dave Taylor has been involved with UNIX since he first logged in to the
on-line network in 1980. That means that, yes, he's coming up to the
30-year mark now. You can find him just about everywhere on-line, but start
here: www.DaveTaylorOnline.com. In addition to all
his other projects, Dave is now a film critic. You can
read his reviews at www.DaveOnFilm.com.