30 January, 2009

Imagemagick Kung-Fu

ImageMagick is a suite of image manipulation utilities used to create, edit and convert virtually every popular image file format in existence.

We've recently found ImageMagick useful in generating slide and video presentations for our current program. I'll identify some of the features we've found useful in our trials.

Resizing Images
For presentations it's often useful to resize various original images to a consistent image dimension. To convert the Black Ice album cover image with dimensions of 1007x918, to a dimension of 640x480 use the following command.


$ convert -resize 640x480 ./blackIce-1007x918.jpg /tmp/image.jpg

It's worth noting that during the resizing of the image the aspect ratio of the source image is preserved. As a result, the dimensions of the destination image may differ from (but will be no larger than) the specified 640x480 dimension. In this instance to achieve a 640x480 image, the width reduction ratio of 1007/640 = 1.5734375 and height reduction ratio of 918/480 = 1.9125 the resizing uses the larger reduction ratio of 1.9125. The dimensions of the destination image results in 527x480 (1007/1.9125)x(918/1.9125).

Rotating Images

$ convert -rotate 45 ./blackIce-1007x918.jpg /tmp/image.jpg


Overlaying Images
Overlaying images to create a composite image is useful in presenting two perspectives simultaneously. The ordering of source images is relevant, specified in a bottom-to-top ordering. For instance, we can overlay an image of Angus and Brian over the Black Ice cover by specifying:


$ convert -composite ./blackIce-1007x918.jpg ./acdc-560x395.jpg /tmp/image.jpg

The resultant image consists of a composite image achieved by overlaying Brian and Angus over the Black Ice cover. It's important to specify the larger of the images as the bottom image, otherwise no overlay will result and you'll get a copy of the top image cropped to the size of the bottom image.


The gravity feature defines where the image will be overlayed on the background image. NorthWest, North, NorthEast, West, Center, East, SouthWest and SouthEast are the available gravity types. Unless specified the default gravity used is NorthWest. Examples of North gravity and SouthWest follow.

$ convert -gravity North -composite ./blackIce-1007x918.jpg ./acdc-560x395.jpg /tmp/image.jpg



$ convert -gravity SouthWest -composite ./blackIce-1007x918.jpg ./acdc-560x395.jpg /tmp/image.jpg


You have even greater control specifying an exact overlay position with the -geometry feature. For example, you can position the overlayed image at x position 200, y position 40 by specifying:

$ convert ./blackIce-1007x918.jpg ./acdc-560x395.jpg -geometry +200+40 -composite /tmp/image.jpg



Blending Images
You can blend two images together, giving an almost transparent overlay by using the composite command. For instance, you can blend Angus and Brian with the Black Ice label using the command:


$ composite -blend 50 ./acdc-560x395.jpg ./blackIce-1007x918.jpg -geometry +10+0 /tmp/image.jpg





$ composite -blend 50 ./blackIce-1007x918.jpg ./acdc-560x395.jpg -geometry +10+0 /tmp/image.jpg




Stretching both images to a known dimension of 1007x918 gives a more uniform overlay of both images.


$ composite -blend 50 ./acdc-560x395.jpg ./blackIce-1007x918.jpg -geometry +10+0 -resize 1007x918 /tmp/image.jpg



The top image is resized to the underlying image, so if the underlying image is larger the top image is stretched to fit. If, however, the top image is larger than the underlying image the top image is cropped to the dimensions of the underlying image.

Now, young warrior, take these weapons to the masses.

No comments: