❝

TL;DR: ImageMagick crops, resizes, converts, and reads image metadata. Two things may catch you: the version split that changes the command name, and the crop geometry syntax.

The preparation step before any pipeline hands an image to the next command needs to do things like crop, resize, convert format, and read metadata. ImageMagick covers all of it, and because it runs on both macOS and Linux, one tool serves a pipeline across platforms.

Installing ImageMagick

Install it on macOS using brew install imagemagick and on Debian or Ubuntu using sudo apt install imagemagick.

Check which version you get installed, before scripting around it (magick -version), because ImageMagick version 7 collapsed everything into one magick command with subcommands, while version 6 ships standalone convert and identify instead. So, you convert using magick image.png ... in version 7 and convert image.png ... in version 6. And you identify an image using magick identify image.png in version 7 and identify image.png in version 6. Adapt to your situation, knowing that the arguments are identical, only the command names differ.

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
900 220
PNG

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

Cropping with -crop and the geometry syntax

As seen in the previous issue on Tesseract, cropping is the preparation step that makes OCR work when text sits in a corner of a large frame. The region is one argument in the geometry syntax:

magick 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:

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.

Resizing with -resize and the exact-dimensions switch

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

magick 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 magick identify on that result gives:

small.png PNG 200x49 200x49+0+0 8-bit ...

A trailing ! forces exact dimensions and distorts:

magick screenshot.png -resize 200x200! distorted.png

You can verify the result using identify:

distorted.png PNG 200x200 200x200+0+0 8-bit ...

If your pipeline does OCR, distorted proportions make OCR worse, so the bounding box is usually what you want, and it is the default.

Converting image formats

Format conversion is done using the input and output filenames; there is no separate flag:

magick 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.

Where this leaves you

Use identify to check what a file is, -crop with the width x height + left + top geometry to isolate the part that matters, -resize with the bounding box to shrink it, and the output extension to convert it.

❝

ImageMagick is not in the book, but it is a natural addition to The Modern CLI Stack for anyone whose pipeline has to hand images to the next command on both macOS and Linux. If you want the full toolkit, The Modern CLI Stack is a free ~50-page PDF + EPUB covering mise, starship, zoxide, fzf, broot, ripgrep, fd, bat, eza, delta, tldr, atuin, lazygit.

Reply

Avatar

or to participate