TechTip: Manipulating Images with ImageMagick

Development Tools
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

Edit, convert, and manipulate images with the free and open-source ImageMagick software.

 

Editing images can be quite a task if you're using a GUI tool like GIMP or Photoshop, especially if you have dozens, or even hundreds, of images to format. With a GUI tool, you might need to edit one image at a time, but ImageMagick provides some great command-line tools to speed up this process.

 

ImageMagick can be installed from source on both Windows and Linux systems. Also, binaries exist for UNIX, Mac OS X, and Windows systems. On RHEL/CentOS systems, ImageMagick is part of the base installation packages and can be installed using yum. If you're using other Linux distributions, look up your package manager and search for ImageMagick packages.

Features and Formats

Currently, over 100 image formats are supported by the ImageMagick tool set. These include everything from DOT (Graph Visualization), JPEG, TIFF, PNG, and BMP to raw formatting from Nikon and Olympus cameras. For a complete list of all the formats, visit the ImageMagick Web site.

 

If you don't want to use the command line tools directly, ImageMagick can be utilized using one of many programming interfaces. This allows you to easily automate image manipulation by incorporating it into many popular programming languages.

 

Many of the features ImageMagick is capable of are very basic, but common, tasks. Resizing, cropping, rotating, trimming, and converting between two different types of format are just some of the basics. It also supports editing transparency, decoration of images, and additions of shapes and text into images. Virtually anything you can possibly do to an image, ImageMagick can handle. For some examples of what ImageMagick is capable of, have a look at their examples page.

Command-Line Tools

ImageMagick offers 11 command-line tools. I won't go over all of them, but the list can be found on the Web site. One of the most-used commands is convert, which allows you to resize, crop, blur, draw on, flip, and much more. Another useful command is the compare command, which does exactly what you expect; it compares the differences of images.

 

Let's get to some examples of using the ImageMagick tools for practical purposes.

 

And remember: before editing any images, always have a backup of your images in case anything goes wrong.

Resizing Images

Have you ever found you had dozens of images that need to be resized? Using convert and a simple Linux for loop, you can easily accomplish this task on the command line. My example uses JPEG images, but substitute your image format for your needs.

 

# cd /path/to/images

# mkdir resized_50

# for image in *.jpg; do convert -resize 50% $image resized_50/$image; done

 

Now you have an entire directory of the resized images. Depending on how many images you have, it might take a bit of time to complete as you watch your CPU spike.

Compare

Now that you resized your image, you can use another ImageMagick tool, compare.

 

# compare -verbose image.jpg resized_50/image.jpg difference

image.jpg JPEG 2816x2112 2816x2112+0+0 DirectClass 1.6mb 0.530u 0:02

resized_50/image.jpg JPEG 1408x1056 1408x1056+0+0 DirectClass 3.8e+02kb

compare: image size differs `image.jpg'.

 

For a complete list of parameters for the compare command, run command with the -help switch. You can see the size differences that we made between the two images after running the convert resize command.

Flip and Flop

To flop an image is to create a mirror image, and to flip is to turn the image vertically. The respective switches are, as expected, -flip and -flop.

 

# convert -flop image.jpg flop_image.jpg

# convert -flip image.jpg flip_image.jpg

Coloring Tips

Another useful feature is the ability to manipulate coloring aspects of images. One example is converting images into the sepia-toned look. To convert a regular color image to the sepia look, run convert with the sepia-tone tag and the percentage of tone you want.

 

# convert -sepia-tone 60% image.jpg sepia_image.jpg

 

Coloring enhancement can be attempted using the normalize feature of convert. ImageMagick will try to span the full range of colors found in the image and then enhance them.

 

# convert -normalize image.jpg normalized_image.jpg

 

Coloring changes can be achieved with the opaque option so that colors can be switched from one to another. If you wanted to change a red color to a green color, this command would do the trick.

 

# convert -fill green -opaque red image.jpg

Convert Image Extension

Converting an image extension from one to the other is a simple process. To convert a JPEG to a PNG file, simply run convert with the respected file names and extensions.

 

# convert image.jpg image.png

Converting Text to Images

One of the coolest features of ImageMagick is its ability to turn text into images. This is really nice for making banners, headers for presentations, or even Web sites. First, get a listing of what fonts you have installed that ImageMagick can see and use. Which version of ImageMagick you have determines which command to run. Try the first one; if that doesn't yield a list, run the second command.

 

# convert -list type

# convert -list font

 

Now that you have a list of fonts you can choose from, create a GIF file with some text converted over to an image. For a complete list of acceptable colors and proper color use, check out the color page on the Web site.

 

# convert -background peru -fill black -font A.C.M.E.-Secret-Agent-Regular -pointsize 70 label:"Max Hetrick" label.gif

 

You should now see an image with the text you specified displayed, as shown below.

 

112108Hetricklabel.gif

 

 

 


  

Endless Possibilities

As you can see, ImageMagick is one of the most versatile command-line image editing tools available. The examples I've covered only scratch the surface of the options at your disposal. Remembering the for loop example above might save you a lot of time on any of the examples demonstrated. For more usage examples than you know what to do with, have a look at the Examples of ImageMagick Usage page.

 

ImageMagick can satisfy everyone's needs--from the image hobbyist to the professional photographer.

BLOG COMMENTS POWERED BY DISQUS