Google
 

Sunday, November 18, 2007

Magic of ImageMagick

Sometime back i'd written a blog about batch image resizing in linux. The basic concept of doing that was combining djpeg/cjpeg command in linux. But, well, screw that! I've a better alternative - ImageMagick.

The main drawback of using djpeg/cjpeg command was that it didn't retain the image metadata, aka EXIF (Camera name, date, Exposure, etc). So as a better and flexible alternative, Imagemagick is our tool of the trade here..

To install Imagemagick in linux,

   apt-get install imagemagick

Imagemagick is also available for Windows, Mac, FreeBSD and any platform you can think of as it is Open Source. Go to www.imagemagick.org for more.

Now the command to resize an image is (in single line)
   convert <original_image> -resize <resize_value>
-quality <resize_value> <output_image>
where
<resize_value> can be like 50% or 800x600.. hope u got the point
<quality_value> can be like 75, 85 (ie, 0-100). This whole quality control can be taken off, in that case ImageMagick automatically sets the best value.

To resize all the images in a folder, we use the following script

#!/bin/sh

mkdir resized

for i in *.JPG
do
echo -n "Converting $i... "
convert $i -resize 50% resized/$i
echo "Done"
done

When this script is run in a folder containing images, it creates a folder named resized and puts all the resized image there, plus the EXIF data are retained too.. Cool huh?

1 comment:

  1. Thank You very much for the mass_resize script. I had a folder with over 100 images to resize and it was very easy with your script. I changed just 50% to 800x600 and within a minute they were all ready to mail for my family and friends. Thanks ones again, really useful.

    ReplyDelete