DEV Community

Cover image for ImageMagick: the image preparation tool that runs everywhere
Kamon Ayeva
Kamon Ayeva

Posted on

ImageMagick: the image preparation tool that runs everywhere

Pipelines that handle images usually need to read metadata, crop, resize, or convert format before handing off. ImageMagick covers all of these needs, and it runs on both macOS and Linux.

Installing ImageMagick

ImageMagick can be installed using Homebrew (macOS) or apt (Debian, Ubuntu):

# macOS
brew install imagemagick

# Debian / Ubuntu
sudo apt install imagemagick
Enter fullscreen mode Exit fullscreen mode

On macOS, Homebrew installs ImageMagick 7 and the leading command is magick (try magick -version to verify). On Debian and Ubuntu today, apt install imagemagick ships ImageMagick 6, so magick is not on the PATH; instead you call the standalone commands directly (identify, convert, mogrify, display). If you want ImageMagick 7 on Linux, you need a third-party source (a PPA, the ImageMagick.org binary tarball, or building from source); the rest of this article flags the version split where it matters.

Regarding version 7 vs version 6, ImageMagick 7 collapsed everything into one magick command, with the older verbs becoming subcommands (magick identify, magick convert, magick mogrify). Version 6 ships those as standalone commands. The arguments to the verbs are the same; only the leading command name changes. One thing to know if you inherit an older script: magick convert … is deprecated in v7 (it still runs but prints a warning). The bare magick in.png out.jpg form used throughout this article is the supported one.

Here is a comparison table for the main operations:

Operation ImageMagick 7 ImageMagick 6
Read metadata magick identify image.png identify image.png
Convert format magick image.png image.jpg convert image.png image.jpg
Resize magick image.png -resize 200x200 out.png convert image.png -resize 200x200 out.png

Reading image properties with identify

When pointed to an image file, identify reads the image properties:

magick identify -format "%w %h\n%m\n" screenshot.png
Enter fullscreen mode Exit fullscreen mode

Or, on version 6, with the standalone command:

identify -format "%w %h\n%m\n" screenshot.png
Enter fullscreen mode Exit fullscreen mode

This gives the output:

900 220
PNG
Enter fullscreen mode Exit fullscreen mode

In the command, the -format switch gives you machine-readable output for scripts, unlike the default listing; %w is the width, %h the height, and %m the format.

Cropping with -crop and the geometry syntax

Cropping is frequently used and it is the preparation step that makes OCR work when text sits in a corner of a large frame. The region is a single argument in the geometry syntax:

magick screenshot.png -crop 400x200+250+10 crop.png
Enter fullscreen mode Exit fullscreen mode

Or, on version 6:

convert screenshot.png -crop 400x200+250+10 crop.png
Enter fullscreen mode Exit fullscreen mode

That syntax is: width x height + left + top. On a 900x220 image, that cuts a 400x200 region starting 250 pixels from the left and 10 from the top. Confirm that by running an identify (magick identify or identify) on the file; you get:

crop.png PNG 400x200 400x200+250+10 8-bit ...
Enter fullscreen mode Exit fullscreen mode

One thing in the syntax catches people: the offsets come left-then-top, +250+10, where many expect the vertical one first.

If the crop comes back as the wrong region, the offset order is the first thing to check.

A related switch is -gravity, which changes which corner the offsets are measured from — the default is NorthWest (top-left), but -gravity Center makes +X+Y count from the center, which is handy for centering a fixed-size crop on a known point.

Resizing with -resize and the exact-dimensions switch

To resize an image, you use the magick command with -resize, as follows:

magick screenshot.png -resize 200x200 small.png
Enter fullscreen mode Exit fullscreen mode

Or, on version 6:

convert screenshot.png -resize 200x200 small.png
Enter fullscreen mode Exit fullscreen mode

Here -resize 200x200 treats the numbers as a bounding box, so a 900x220 image comes out as 200x49, with proportions kept; calling identify on that result gives:

small.png PNG 200x49 200x49+0+0 8-bit ...
Enter fullscreen mode Exit fullscreen mode

Adding a trailing ! forces exact dimensions, and you end up with a stretched result (the same 200x49 image, now squashed into 200x200):

magick screenshot.png -resize 200x200! stretched.png
Enter fullscreen mode Exit fullscreen mode

To confirm, calling identify on that result gives:

stretched.png PNG 200x200 200x200+0+0 8-bit ...
Enter fullscreen mode Exit fullscreen mode

For OCR pipelines, distorted proportions hurt recognition, so keep the bounding box default unless you have a reason to stretch.

Converting image formats

In some cases, you might need format conversion, which is done using the input and output filenames; there is no separate flag:

magick screenshot.png screenshot.jpg
Enter fullscreen mode Exit fullscreen mode

Or, on version 6:

convert screenshot.png screenshot.jpg
Enter fullscreen mode Exit fullscreen mode

The result identifies as JPEG:

screenshot.jpg JPEG 900x220 900x220+0+0 8-bit ...
Enter fullscreen mode Exit fullscreen mode

That's it! magick reads the format from the extension of the last argument.

Then you can add the -quality N switch (where N is 1-100) to control the lossy-compression level for JPEG and WebP outputs; the default is 92, and dropping to -quality 75 roughly halves the file size with a small visible difference.

Also, using the -strip switch removes special metadata, such as EXIF, ICC, or IPTC, on the way out, which is important when the output is going into a pipeline rather than a photo library and you don't want to leak the source tool's metadata.

Batch processing with mogrify

For pipelines that run over a directory of files, ImageMagick has mogrify, which is nice when you want to apply a transform in place. For example:

magick mogrify -resize 400x *.png
Enter fullscreen mode Exit fullscreen mode

Or, on version 6:

mogrify -resize 400x *.png
Enter fullscreen mode Exit fullscreen mode

This shrinks every PNG in the current directory to a 400-pixel-wide bounding box, replacing each file as it goes.

The command differs from magick (or convert on version 6) in one key way: mogrify always writes back to the input file. For batch processing with separate output filenames, write the loop yourself and call magick (or convert) inside it.

What this overview covered

Let's recap the operations discussed:

  • magick identify reads what a file is (width, height, format).
  • magick with -crop, followed by WxH+X+Y geometry, isolates the part of the image you select.
  • With -resize and a WxH bounding box, the conversion module shrinks the image while keeping proportions; the WxH! variant forces exact dimensions and you get distorted proportions.
  • For format conversion, the output file's extension is what the magick command reads to pick the format coder (there is no separate flag); -quality N tunes lossy compression for JPEG and WebP outputs, and -strip removes EXIF, ICC, and other metadata on the way out.
  • magick mogrify applies any of those transforms to a directory of files in place, with no separate output name.

This article expands issue #17 of my newsletter, Shellcraft — one piece of terminal craft per week.

Top comments (0)