oreilly.comSafari Books Online.Conferences.


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 <.

Pages: 1, 2

Next Pagearrow

Linux Online Certification

Linux/Unix System Administration Certificate Series
Linux/Unix System Administration Certificate Series — This course series targets both beginning and intermediate Linux/Unix users who want to acquire advanced system administration skills, and to back those skills up with a Certificate from the University of Illinois Office of Continuing Education.

Enroll today!

Linux Resources
  • Linux Online
  • The Linux FAQ
  • Linux Kernel Archives
  • Kernel Traffic

  • Sponsored by: