Tuesday, July 17, 2007

magic with ImageMagick

ImageMagick is a set of command line utilities for manipulating images, very useful for performing some transformation on multiple images (For individual images, visual editors like GIMP may be a better choice).

The problem


A friend of mine had 57 images (of 2272x1704 !!!) consuming 61MB of disk space. We had to find a way to resize all of them to 1024x768.


A solution


Once I got all 57 images into my_images, the following commands did the job.


$cd my_images
$mkdir conv
$for i in *.jpg
>do
>convert -resize 1024x768 $i conv/$i
>done


And now 6MB is enough..

The convert command is just one of many tools provided by ImageMagick. It converts an input image to create an output image according to the options given to it.

The syntax is

convert options infile outfile

The resize option is used to...(you know what)

More info..



convert has got like a million options (for rotating, blurring, cropping, etc..).One option even allows to specify an affine transformation matrix to transform the image(Phewwww, Who's going to use that).

Another cool tool is montage which is used to create a composite image from many images.

For ex:-
To create an image that consists of a thumbnail preview of all 57 images in my_images..


$montage -geometry 128x128+8+8 -tile 8x8 -background gray *.jpg conv/all.jpg


The syntax is

montage options infile1 infile2 ... infilen outfile

The geometry option specifies size of each tile (128x128) and spacing between tiles(+8+8).

The tile option specifies no: of rows of tiles and no: of tiles in each row.

The choice of 8x8 for tile option is not arbitrary. It allows a maximum of 64 tiles in one file (and I want all 57 thumbnails in one file), if there are more than 64 input files then thumbnails will be split across multiple files.

No comments: