Pages

Wednesday, April 22, 2009

Using Image Magick in a Crunch

So you just got a bunch of images from a client and you need them converted tonight before you can go to sleep. You really don't want to prep each image manually. Whatever shall you do?

Think Image Magick. It's a potent package of image processing goodness with an antiquated name, but I promise you'll get over the name.

This situation happened to me last night. I had a bunch of TIFF files and no time to process them in to Web-ready JPGs. So I created the following shell script:

# Create web versions of product labels and 
# copy them into the appropriate directory.
list=`ls --color=never *.tiff`;
for image in $list
do
    product_id="`echo $image | awk -F "." '{print $1}'`";
    echo "convert $image -resize \"50%\" label.jpg;";
    convert $image -resize "50%" label.jpg;
    mv label.jpg /path/to/products/$product_id/;
done

The convert command is part of the Image Magick package. All of the images were named in the form of product_id.tiff, so that helped. Also, there many other options you could investigate with the convert command, so some experimentation might be required. However, since you've just scripted it, the experimentation becomes very easy.

I observe that even a trivial number of images might be worth scripting in this manner; this script took just a few minutes to write (even looking at the man page for convert). Plus, I knew I would be getting more images later. Plus, plus, I later had to resize the images which was easy using the script.

No comments:

Post a Comment