Manipulate Images with ImageMagick

In my last article, I had some fun looking at the children's game of rock, paper, scissors, writing a simple simulator and finding out that some strategies are better than others. Yes, I used "strategy" and "rock, paper, scissors" in the same sentence!

So for this article, I thought it would be interesting to delve into something more functional and pragmatic: image manipulation. Ordinary shell scripts don't tend to do much with images because you can't display anything from the command line.

But let's be honest here. The chance that you're running Linux or a similar command-line interface raw on a computer terminal is pretty darn low. More likely, you've got a terminal window open on your X11 system or, like I often have, you're running a command-line interface app within a modern OS like Mac OS X. And this means, yes, you do have the ability to display graphics, just not within the terminal app itself.

Get Yourself a Copy of ImageMagick

The first step is to download and install a copy of the ImageMagick suite of graphics-related commands. You already might have it installed if you're lucky: Just type convert -version, and if you have it installed, you'll see something similar to this:


$ convert -version
Version: ImageMagick 6.9.6-6 Q16 x86_64 2016-12-31
 ↪http://www.imagemagick.org
Copyright: Copyright (C) 1999-2016 ImageMagick Studio LLC
License: http://www.imagemagick.org/script/license.php
Features: Cipher DPC Modules
Delegates (built-in): bzlib djvu fftw fontconfig freetype gslib
 ↪jbig jng jp2 jpeg lcms ltdl lzma openexr png ps tiff
 ↪webp x xml zlib

If you don't have it installed, it can be quite a task to get it all up and running. Everything lives here, which is where you want to get started.

On a Linux system, you can use the package manager of choice for your distro. You can grab a compressed tar image from the site, or you can use rpm, like this:


rpm -Uvh ImageMagick-7.0.4-1.x86_64.rpm

Of course, there's a bit more to it, but that'll get you started.

On a Mac, you'll want to start by installing MacPorts, which you can't do until you install Xcode (free from Apple, get it through the App Store). Once you've installed Xcode and MacPorts, you can install ImageMagick, and you're good to go.

You know you're good to go when the test command convert -version returns something meaningful. As always, when you install new software, you'll want to log out and log in again for the PATH changes and shell command-line hash to include all the newest programs.

Converting Image Formats

One of the most useful tasks ImageMagick can help you with is converting image file formats. It's a remarkably well built suite of programs and can read or write more than 200 different formats. Don't believe me? Try this command:


convert -list format | more

Among the most common formats that you'll actually encounter in your day-to-day computer usage are the following:

  • BMP: MS Windows bitmapped image.

  • GIF: Graphics Interchange Format.

  • JPG: JPEG image format.

  • PNG: Progressive Network Graphic format.

  • TIFF: Tagged Image File Format.

ImageMagick knows oodles of other formats too, including all the major video formats (MKV, MP4, AVI, MOV). It also can convert things like EPSF (Encapsulated Postscript) and even PDF (Portable Document Format), which can be useful in specific instances.

______________________

Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.