Linux DevCenter    
 Published on Linux DevCenter (
 See this if you're having trouble printing code examples

Living Linux

Transforming Images


There are many tools for Linux which can be used to transform or manipulate images in particular ways. One of the most versatile of these is the ImageMagick suite of imaging tools (available in the Debian imagemagick package, or from here).

One of these ImageMagick tools, mogrify, is particularly useful for performing fast command-line image transforms -- use it to scale the size of images, rotate images, and reduce colors in an image. It always takes the name of the file to work on as an argument, and it writes its changes to that file. Use a hyphen, -, to specify the standard input, in which case it writes its output to the standard output.

Sample Image

Let's take the sample image at the right and put it through the works. What can we do to it from the command line?

Changing the size of an image

To scale an image with mogrify, use the -geometry option with the width and height values, in pixels, as an argument.

For example, to rescale penguin.jpeg to 250x200 pixels, type:

mogrify -geometry 250x200 penguin.jpeg

This changes the original image to:

Mogrify used to change the height and width of the image

When mogrify rescales an image, it maintains the image's aspect ratio, so the specified width and height are only maximum values -- to force a particular image size without necessarily preserving aspect ratio, append the geometry with an exclamation point.

To rescale penguin.jpeg to exactly 250x200 pixels, forcing these values regardless of aspect ratio, type:

mogrify -geometry 250x200! penguin.jpeg

This changes the original image to:

Using mogrify with the ! option to force height and width regardless of aspect ratio

You can also change width and height by a percentage of its current value; to decrease by a percentage, give the value followed by a percent sign. To increase by a percentage, give the value plus 100 followed by a percent sign -- to increase by 25%, for example, specify a value of 125.

For example, to increase the width of the original penguin.jpeg by 5% of its current size and increase its height by 10% of its current size, type:

mogrify -geometry 105%x110% penguin.jpeg

Resulting in:

Increasing dimensions using percentage values

Rotating an image

To rotate an image, use the -rotate option followed by the number of degrees to rotate by. If the image width exceeds its height, follow this number with a >, and if the height exceeds its width, follow it with a <. Since both < and > are shell redirection operators, enclose this argument in quotes. (Omit either if the image height and width are the same.)

For example, to rotate by 90 degrees the original penguin.jpeg, whose height exceeds its width, type:

mogrify -rotate '90<' penguin.jpeg

This changes the penguin.jpeg image to:

Results of rotating the image.

This new penguin.jpeg image has a width that exceeds its height, so to rotate it again, you'd use > instead of <.

Reducing the colors in an image

To reduce the number of colors in an image, use the -colors option followed by the number of colors to use.

To reduce the colors in the original image to two, type:

mogrify -colors 2 penguin.jpeg

This changes the penguin.jpeg image to:

Two color version of the original image

To reduce the colors with Floyd-Steinberg error diffusion, add the -dither option; dithering an image typically improves image quality when the number of image colors must be reduced. You can also use the -map option with a second filename as an argument to use the set of colors in the second file as the "color map" used in the first.

To reduce the number of colors in the original penguin.jpeg to four and dither the image, type:

mogrify -colors 4 -dither penguin.jpeg

Resulting in:

A foour-color dithered version.

Use the -monochrome option to turn a color image into a monochrome one.

For example, to make the original penguin.jpeg file monochrome, type:

mogrify -monochrome penguin.jpeg

Annotating an image

Use the -comment option followed by a comment in quotes to annotate a JPEG image file with a comment. This option is useful for adding copyright (or copyleft) comments in a file, or for annotating a file with a URL.

For example, to annotate our sample file, type:

mogrify -comment 'See' penguin.jpeg

You don't see the annotation when you view the image itself; the annotation is added to the image header in the file. You can read annotations with tools that display information about an image file, such as display or the GIMP; for JPEG files, you can also use the rdjpgcom tool -- it outputs any comments in the JPEG file given as an argument.

To read any comments saved in the image file penguin.jpeg, type:

rdjpgcom penguin.jpeg

Adding borders to an image

To draw a border around an image, use the -border option followed by the width and height, in pixels, of the border to use.

For example, to add a border two pixels wide and four pixels high to the original penguin.jpeg, type:

mogrify -border 2x4 penguin.jpeg

This changes the penguin.jpeg image to:

With added border.

Note that the border is appended to the outside of the existing image; that is, none of the existing image is cropped to add the border.

The -frame option works just like -border, but it gives a more "decorative" border to an image.

For example, to add a decorative frame twenty pixels wide and twenty pixels high to the original penguin.jpeg, type:

mogrify -frame 20x20 penguin.jpeg

This changes the penguin.jpeg image to:

Next week: using a scanner with Linux.

Michael Stutz was one of the first reporters to cover Linux and the free software movement in the mainstream press.

Read more Living Linux columns.

Copyright © 2009 O'Reilly Media, Inc.