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
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
Or, on version 6, with the standalone command:
identify -format "%w %h\n%m\n" screenshot.png
This gives the output:
900 220
PNG
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
Or, on version 6:
convert screenshot.png -crop 400x200+250+10 crop.png
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 ...
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
Or, on version 6:
convert screenshot.png -resize 200x200 small.png
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 ...
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
To confirm, calling identify on that result gives:
stretched.png PNG 200x200 200x200+0+0 8-bit ...
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
Or, on version 6:
convert screenshot.png screenshot.jpg
The result identifies as JPEG:
screenshot.jpg JPEG 900x220 900x220+0+0 8-bit ...
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
Or, on version 6:
mogrify -resize 400x *.png
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 identifyreads what a file is (width, height, format). -
magickwith-crop, followed byWxH+X+Ygeometry, isolates the part of the image you select. - With
-resizeand aWxHbounding box, the conversion module shrinks the image while keeping proportions; theWxH!variant forces exact dimensions and you get distorted proportions. - For format conversion, the output file's extension is what the
magickcommand reads to pick the format coder (there is no separate flag);-quality Ntunes lossy compression for JPEG and WebP outputs, and-stripremoves EXIF, ICC, and other metadata on the way out. -
magick mogrifyapplies 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)