Want a powerful, scriptable bitmap image editor that creates, edits, and converts images? ImageMagick is a suite of 11 commands and a GUI that let you resize, flip, mirror, rotate, distort, and shear pixel-based images. With it, you can adjust colors, create special effects, and draw text, lines, polygons, ellipses, and Bezier curves. ImageMagick supports more than 100 image file formats, and offers APIs for all popular programming languages.
ImageMagick is a useful tool for webmasters who need to create web photo galleries, resize images and create thumbnails, and automatically resize user-uploaded images. In brief:
Want to see just how powerful ImageMagick is? Let's start by trying some useful commands to resize images – but before you begin working on an image with ImageMagick, make sure you have it backed up.
The convert command syntax, as explained in man convert, is convert [input-options] input-file [output-options] output-file. Input and output options are, um, optional, but filenames are required. This simple example resizes a PNG file:
$ convert image.png -resize 150x100 image-s.png
ImageMagick GUIImageMagick also includes a simple GUI interface that you open with the display commmand. Run display with no options to bring up an ImageMagick screen with its wizard logo. Left-click on this screen to open the complete menu; right-click to open a Shortcut menu. Or you can open an image directly with display imagename (see below – click to enlarge).
If you give your input and output files the same names then your original image will be overwritten with the output of the command. If you give them different names then your original file will not be changed, and you will have two files. The resize value of 150x100 is called geometry. Geometry is a big deal in ImageMagick, so let's take a moment to make sure we understand it. Width and height values are always in pixels. When both width and height values are given, the width value always comes first. The geometry values must have no spaces, so 150 x 100 would not work. In our simple example 150x100 means "These are the maximum sizes, and preserve the aspect ratio." In other words, ImageMagick will fit your image into a 150x100 pixel rectangle by altering the width and height dimensions, without changing the aspect ratio. It may not result in an image that's exactly 150x100 pixels, but it will be as close as it can get.
If you want to specify the exact width and preserve the aspect ratio, then specify only the width:
$ convert image.png -resize 150 image-s.png
To set the exact height and preserve the aspect ratio, specify only the height value prefixed with x, like this:
$ convert image.png -resize x100 image-s.png
When you need your image to be an exact size, without preserving the aspect ratio, append an exclamation point to the geometry:
$ convert image.png -resize 100x100! image-s.png
You can tell ImageMagick to only resize images that are larger or smaller than your geometry by using the greater-than and less-than signs. The command below resizes only images that are larger than your geometry:
$ convert image.png -resize 64x64> image-s.png
And this resizes only images that are smaller:
$ convert image.png -resize 64x64< image-s.png
One more useful option is resizing by percentage. Values under 100% reduce the size of your image, and values over 100% increase it. This example doubles the image size:
$ convert image.png -resize 200% image-s.png
Depending on which command shell you are using, you may need to escape the exclamation point, greater than, less than, and percent signs.
Creating thumbnails with ImageMagick is so easy you will fall over in happiness. This example creates 100-pixel-wide thumbnails of all the JPG files in the current directory. Your originals are preserved and the thumbnails retain the original filenames with sequential numbers appended – image-0.jpg, image-1.jpg, and so on:
$ convert -thumbnail 100 *.jpg
The thumbnail option is special because it does more than just resize the images; it is optimized for speed.
The convert command can also convert file formats. The simplest way to use it is:
$ convert image.png image.jpg
You can fine-tune this any number of ways, such as by specifying size and .jpg compression level:
$ convert image.png -resize 240x320 -quality 85 image.jpg
JPG uses lossy compression, and possible values are 1 to 100, with higher values resulting in better-quality images with larger file sizes. The default is the same as your input image, if ImageMagick can determine what it is; otherwise it is 92.
PNG compression levels range from 0 to 9, from least to most. PNG compression is lossless, so higher levels of compression do not change image quality. While this sounds simple, there are many fine points to PNG compression that you can read about if you are interested in maximum control and squeezing files as small as possible.
In fact, to you can learn more about any aspect of ImageMagick from its documentation. Each command has its own man page, and the master man page is man imagemagick. Extended documentation online includes:
Even though the documentation is large and thorough, it could be better organized. Many ImageMagick howtos tend to gloss over essential details, which is understandable because ImageMagick has hundreds of command options, and finding important details often requires some detective work. But once you master the important fundamentals the whole works makes sense, and you are on your way to becoming an ImageMagick ninja.
Your next step should be a look at my next article on using ImageMagick to do things in batches.
Allowed tags: <a> link, <b> bold, <i> italics