Transforming Images
07/11/2000There 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.
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:

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:

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:
![]()
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:
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 |