Quick Reference — Converting Images with ImageMagick

  • July 28, 2010
  • Avatar for peter
    Peter
    Upfold

ImageMagick logo

While you might associate working with images with big graphical programs like GIMP or Photoshop, ImageMagick is an entirely different animal. It is a suite of command-line programs for converting and manipulating images.

But why would you use a command line program to work with images, which are by definition, graphical? Well, for a start, you don’t have to load up a slow (and dare I say bloated) graphical app just to achieve a simple conversion task: you can fire off the command and have the result done in no time. Also, you can go to pretty much any Linux computer and know that ImageMagick is available — its ubiquity makes it a useful tool to know how to use.

So let’s jump right in to doing some simple, but useful, conversion tasks with ImageMagick.

Change format of a single image

If you have, say, a PNG file that you want in JPEG format, you can simply run convert with the source filename and the desired destination filename, and it will infer the desired format from the destination’s file extension.

For example:

convert source.png destination.jpg

Resizing images

To resize an image with ImageMagick, you can use the -resize command line switch to resize by either a percentage, or with the exact desired dimensions. Note that resize will preserve the aspect ratio of your image, even if you choose exact dimensions that are in a different ratio.

convert source.jpg -resize 75% destination.jpg

convert source.jpg -resize 800x600 destination.jpg

Apply heavier compression to an image

If you want to squeeze down the file size of an image in a lossy compressed format, such as JPEG, you can instruct convert to change the quality attribute.

convert big_file.jpg -quality 60% small_file.jpg

Batch converting from one format to another

So, say you have a folder full of PNG files that you now need in JPEG format. With many applications, you would have to go through the laborious process of opening each image and using Save as to save the file in the new format.

With ImageMagick and the command line, you can convert all the PNG files in the current working directory to JPEG like this:

mogrify -format jpg *.png

Trim off the edges of an image

If an image has too much space around the edges, you can use trim to automatically crop the image. This feature removes any edges that are exactly the same colour as the pixels at the corner of the image.

convert non_trimmed.jpg -trim trimmed.jpg

And there is more…

We really are only scratching the surface of this powerful suite of applications. There is extensive documentation on all its features and there is certainly more to explore if you find yourself using ImageMagick a lot.

Avatar for peter Peter Upfold

Home » Articles »