Rate this page

Resize an image using ImageMagick

Tested on

Debian (Etch, Lenny, Squeeze)
Ubuntu (Lucid, Maverick, Natty, Precise, Trusty)

Objective

To enlarge or reduce an image using ImageMagick

Scenario

Suppose that the file input.png contains a PNG-format image that is 320 by 256 pixels in size. You wish to resize it to 240 by 192 pixels, writing the result to the file output.png.

Method

The method described here uses ImageMagick to resize the image. On Debian-based systems, the command-line interface to ImageMagick is provided by the package imagemagick:

apt-get install imagemagick

Resizing is performed using the -resize operator, which can be invoked as an argument to the convert command:

convert input.png -resize 240x192 output.png

By default the aspect ratio of the image is preserved. This is done by reducing either the width or the height of the output by an appropriate amount. If you do not want this to happen, and are prepared to accept the resulting distortion, then you can allow the aspect ratio to change by appending an exclamation mark to the required size. Since this is a shell metacharacter it must be escaped:

convert input.png -resize 240x192\! output.png

There are several other ways in which the output size can be specified, for example as a percentage. See the ImageMagick documentation for further details.

The method described here is not limited to PNG-format images: a large number of other formats are supported, including BMP, GIF, JPEG, PNM and TIFF.

Testing

You can check that the result is of the expected size using the identify command, which is also part of ImageMagick:

identify output.png

The third field of the output is the size in pixels:

output.png PNG 240x192 240x192+0+0 DirectClass 16-bit 100.373kb

If the reported size is different from what was requested then that is probably because one of the dimensions was reduced in order to preserve the aspect ratio. See above for how to override this behaviour.

Further Reading