Sunday, March 7, 2010

Convert images with open source ImageMagick

Tools like the GIMP (www.gimp.org) and similar graphical applications are great for modifying and manipulating images. Sometimes, however, they can be overkill for little things that need to be done, such as converting file types or resizing images. As well, a graphical tool can be time consuming and difficult to script, unlike CLI tools.

The ImageMagick suite (www.imagemagick.org) of programs allows you to manipulate images from the command-line, making it much simpler to convert large numbers of pictures or even for that single quick image conversion.

Some of the tools that ImageMagick includes are the convert tool, the composite tool, and the display tool.

The convert tool is used to convert pictures from one format or size to another. The easiest method is to simply convert an image from one format to another using:

$ convert image.jpg image.png

This will convert image.jpg from a JPEG to a PNG and save it as image.png. To convert and resize an image, use:

$ convert -size 50x50 image.jpg image.png

This will convert from JPEG to PNG format and resize the resulting image to 50x50 pixels. Likewise, you can use convert to rotate an image:

$ convert -rotate 90 image.jpg image.png

This will rotate the image 90 degrees and convert it to a PNG file. To manipulate the file and keep it in the same format:

$ convert -rotate 90 image.jpg image-new.jpg

To create a composite image, or layer one image above another, use the composite tool. For instance, to put logo.jpg in the top-left corner of image.jpg, you would use:

$ composite -gravity NorthWest logo.jpg image.jpg composite.jpg

This will layer logo.jpg over image.jpg in the top-right corner (NorthWest), and the resulting file will be named composite.jpg. Each edge of the picture is noted by direction: Top is North, bottom is South, right is East, and left is West. The composite tool respects transparent pixels, so using it is a great way to automatically tag all your photos with your name or Web site address.

These commands merely scratch the surface of what ImageMagick can do. ImageMagick is capable of creating new images, embedding steganography in pictures, and much more.

No comments:

Post a Comment