Performing Image Magic with ImageMagick

Whether you know it or not, there’s a good chance you’ve already used ImageMagick, at least if you’re a Linux user. It’s the image processor behind many graphics-related applications, and for good reason. With this one tool, you can perform dozens of common image manipulations and conversions from the command line or within a script/application. ImageMagick can convert, transform, draw, decorate, animate, and composite images. It can be used from the command line for quick needs or built into a more complex software suite. This guide will cover some of the most “magical” features of ImageMagick and provide examples of how to use it to solve everyday tasks.

A note about convert: The command name convert will be used multiple times throughout this guide. It’s one of several commands to access ImageMagick’s features, not just converting between types. The fact that we begin a command with the word convert does not necessarily mean we are converting from one image format to another.

It’s pre-installed on many Linux systems, so you may not need to worry about installing at all. An easy way to find out is to open a command line and type

which convert

If it gives you a result such as /usr/bin/convert, then you’re all set and can proceed to the next section. If not, it should be available in your distro’s standard repositories. Debian and Ubuntu users could fetch it with

sudo apt-get install imagemagick

ImageMagick is great for performing simple image conversions. Converting a JPG to PNG, for example, is as simple as:

convert myPicture.jpg myPicture.png

It’s important to note that the convert command performs its operations on a COPY of the file. Using convert will not replace or overwrite your original image.

While converting your image, you can add optional flags to perform basic transformations like rotate, resize and crop. As noted above, we may be using the convert command but that doesn’t mean we must choose a different output type.

#Rotating a single image 90 degrees
 
convert -rotate 90 MyPicture.jpg MyPicture-rotated.jpg

imagemagick-rotate

Globbing is a common term for using wildcards (such as “*”) to represent a group of files. ImageMagick supports globbing from the command line, so you can perform operations on a group of files using wildcards. For example, to convert all the JPG files in a directory to PNG, you can use something like this

convert *.jpg image.png

which would create new PNG files, named image-0.png, image-1.png, image-2.png, etc.

ImageMagick’s intelligent command parsing means that it can determine what you likely want to do based on the type of input you specify. In the previous example, we used convert *.jpg image.png and it created an individual copy of each JPG file. What if we were to just replace the “.png” with “.gif”? The answer is not multiple GIF files, it’s an animated GIF composed of the JPG files.

convert *.jpg myanimated.gif

Using ImageMagick, you can even render graphics from text using the command line. In this example we’re specifying background color, text color, font, size, and the text to render. In this case, I’m manually specifying the path to a TTF font.

convert -background lightblue -fill yellow -font /usr/share/fonts/truetype/thai/Purisa.ttf -pointsize 72 label:ImageMagick rendertext.png

imagemagick-rendertext

Sometimes you may need to add some text or a logo/graphic to an existing image. For this you use the -append option. In this example, I’m adding a bar to the bottom of my image with a note about the contents. If I have a file called tux.jpg, I could use the following to append a note to the bottom:

tux_penguin

convert tux.jpg -background Khaki label:'This is Tux!' -gravity Center -append tux_annotated.jpg

imagemagick-annotated

The examples shown here barely scratch the surface of what ImageMagick can do. With a little time and practice, and probably the help of this monstrous guide, you can do amazing things with this program. You’re not limited to the command line, ImageMagick provides APIs for C, C++, Java, .NET, Python, and more. If you’ve got a story of what ImageMagick has done for you, let us know in the comments!