This is a read-only archive. Find the latest Linux articles, documentation, and answers at the new!


CLI magic: import, display, mogrify

By Joe Barr on February 23, 2004 (8:00:00 AM)

Share    Print    Comments   

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.

Portion of window 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.

Share    Print    Comments   


on CLI magic: import, display, mogrify

Note: Comments are owned by the poster. We are not responsible for their content.

Noobies v newbies

Posted by: Joe Barr on February 24, 2004 10:30 PM

The long answer

The first article in this series was dripping with a faux "boot camp drill instructor addressing recruits" kind of attitude. You know, the whole "Whattsa matter, sweetheart, did your mommy forget to pack your favorite jammies?" thing.

For that kind of opening to work, one phrase in particular needed to have grit: nattering noobies. To my ear, the double t's in nattering called out for an alternative spelling which would echo the repetition. "Nattering newbies" just doesn't work as well for me.

The short answer

Yes, I'm weird.<nobr> <wbr></nobr>:)


Mogrify question

Posted by: Administrator on February 24, 2004 12:58 AM
I've been trying to get mogrify to do what I need it to for some time, with no luck.

I need to to auto adjust the colors in my photos. For instance, I recently took some photos that had lots of white in them, from snow. They came out with a strong blue tint to them. When I use gThumb or ACDSee on them, and use the auto color level feature, the photos come out 'normal', with the snow white and the other colors brighter. I would use gThumb all the time, but it deletes my Exif data.

I had thought that mogriphy -normalize or -equalize would do the trick, but neither does. Is there some other switch that will do this, or perhaps combination of switches?



series of articles

Posted by: Administrator on February 24, 2004 07:00 AM

I gotta say, I really enjoy these series of articles. They're broken down into bite-size chunks that seem just right. It's hard for me to know for sure, since I've been using unix-like shells for such a long time, but it seems that this would make for some good start-from-scratch learning material.

Writing about <tt>import</tt> and <tt>mogrify</tt> is a neat idea. I use these tools very often to modify both screenshots, and pictures from my digital camera. This is very handy. The flexibility of modern unix shells makes it even nicer (hopefully these articles will eventually get into some nice looping or conditional structures, if that's not too advanced for the target audience). It might also be a good idea to mention that ImageMagick comes with a few other useful command line tools. A list of these is available in <tt>man imagemagick</tt>. Maybe you'll write some more articles later, but I mention this for folks are seeking more information right away.

One final point I'd like to bring up -- and I hate to be a troll, as I've posted about this before -- but I find it curious that you use the word "noobies" instead of the more common "newbies". Is there any reasoning behind this word choice, or are you simply weird? (Not that being called weird should be taken as much of an insult. How weird am I for noticing and repeatedly mentioning this?)


Re:Noob, noobies, newbies...whatever.

Posted by: Administrator on February 24, 2004 02:33 PM

Hmm. According to Google Groups, "newbies" is not only much more popular, it's also older. The earliest "newbies" result I got is <A HREF="" TITLE="">from 1988</a>, but the earliest "noobies" result is <A HREF="" TITLE="">from 1994</a>. I'm probably risking trollification here, but I don't follow your reasoning. I guess usenet isn't the end-all be-all indicator of word usage, but it's as good as anything I could think of. I think I'll let this subject drop now, though. I've probably annoyed enough people as it is with my off-topic blathering.


Re:Noob, noobies, newbies...whatever.

Posted by: Administrator on February 25, 2004 11:49 AM
LOL....Oh well.
It's not like it really matters now anyway...Gopher being dead and all.<nobr> <wbr></nobr>;)


Noob, noobies, newbies...whatever.

Posted by: Administrator on February 24, 2004 12:01 PM
Joe does do a good always.

To the "noobie" question:
Noobie (noob is the more common short-usage form) comes from the late-'80s/early-'90s, with the more correct spelling of newbie recently coming into popular usage.

Those of us who have been around a while like the quaint nostalgia associated with the original usage; which was actually used to politely account and adjust for the presence of "newbies" in a variety of contexts.

Chalk the old usage up to habit.<nobr> <wbr></nobr>;)


This story has been archived. Comments can no longer be posted.

Tableless layout Validate XHTML 1.0 Strict Validate CSS Powered by Xaraya