CLI magic: import, display, mogrify

302

Author: JT Smith

You might think that working on the command line means you are sentenced to a desktop without graphics or digital images. But that’s wrong thinking. You may also be surprised to learn that some popular GUI applications for image processing can also be used from the CLI. Today we’ll take a look at three tools for capturing, displaying, and massaging images from the CLI. They are all (import, display, and mogrify) part of ImageMagick.

Why bother with import and display?

Why would you want to do image work from the CLI? That’s a good question. I’ve got a good answer. Speed and ease. Let’s do a quick comparison. I love the GIMP, make no mistake about it. One of the things I’ve used it to do over the years is screen captures. The GIMP makes it pretty easy to capture a window or the full screen.

All you need to do is start the GIMP, then click on File->Acquire->ScreenShot. That brings up a dialog window where you can select full screen or specific window, whether or not you want all the window decorations, and set a delay time in seconds by pointing at the up or down arrows provided for that purpose. Then click “OK”. If you’ve chosen to capture a specific window, your next task is to click on it. That’s pretty much all there is to it. Oh, except that then you have to save the image you’ve just captured, but that too is only a few more clicks and the typing of the file name and extension.

To accomplish the same thing from the command line using ImageMagick’s import, simply type this:

import window.png

Then click on the window you want to capture. You’re done. Let’s see, from the command line, that means typing nine characters, hitting enter, and then a single click with the mouse. Using the GIMP, which I truly love, by the way, I am not trying in any way to disparage it, the same task requires at least eleven clicks, plus typing the file name.

Thats one click and ten keystrokes from the CLI, and eleven clicks and nine keystrokes from the GUI. But who’s counting keystrokes and clicks? Elapsed time is a better metric. About two seconds from the command line and fifteen-to-thirty in the GUI. Get the picture? In this comparison, the CLI wins in speed and ease.

More on import

Here are some examples of using import to capture screens and windows. I typed the following in the terminal window shown in that image to capture the entire screen and name the image “import-1.png”:

import -window root cli-image-1.png

But since I’m only interested in one specific window and not the whole screen, this is what I typed to grab the window containing the photo of donkeys.

import cli-image-2.png

Click to see what the single window capture looks like. After looking at the image just captured, I decided that I would like to crop the image just a bit. I wanted to focus on the heads of the three donkeys and lose the window trim.

I decided to use the mouse from the CLI to accomplish this. First I displayed the capture by entering:

display cli-image-2.png

The I repeated the earlier import command, naming the resultant image cli-image-3.png. After entering the import command, instead of simply clicking on the window to grab it, I left-clicked and held the button down, then dragged the mouse pointer from the upper left to the lower right of the region I wanted in the image, and released the mouse button. The image below is the result of that capture.

Want to make it look like a blizzard from the 1890s? Display it using the “-monochrome” option. There are dozens of other tweaks that you can do with import and display, but that’s enough for them here. Let’s move on to mogrify.

What is mogrify?

Mogrify allows you to transform digital images in a number of ways, including (this from the man page) “image scaling, image rotation, color reduction, and others.” Please note that mogrify can work on a single image or on a sequence of images.

For example, if you want to transform all the .tiff images in a directory to .jpg, you can do it with a single command:

mogrify -format jpeg *.tiff

That will convert all the TIFF images in the directory to JPEG, whether it’s a single image or hundreds of them. Feel the power?

Need to make thumbnails? Easy as pie, but be careful with this one. The thumbnails are written over the original file names so always work from backups when making thumbnails. Enter this command in the directory containing the images you want thumbnails of:

mogrify -geometry 120x120 *.jpg

One last comment about these tools from the ImageMagick suite. I recommend going to the ImageMagick website for documentation on using them rather than relying on the man pages. The man pages don’t seem to have been well maintained.