AutoCrop

Trim transparent borders with ImageMagick

The whole thing is one line — but the line most people copy is missing a flag, and the omission fails silently in a way that is genuinely hard to debug.

Trim it in the browser instead

No install, no upload, adjustable threshold for soft shadows.

Open AutoCrop

The command

magick input.png -trim +repage output.png

On ImageMagick 6 the binary is convert rather than magick; everything else is identical. Both flags are required:

FlagWhat it does
-trimFinds the bounding box of the content and crops the image data to it. On an image with an alpha channel, "border" means transparent.
+repageResets the virtual canvas and the crop offset. Without it the file still claims its original size, and the crop is stored as an offset instead of being applied.

Why +repage is not optional

ImageMagick images carry a page geometry: a virtual canvas size plus the offset of the actual pixels within it. After -trim the pixel data is genuinely smaller, but the page geometry still describes the original canvas and records where the trimmed region sat inside it.

The consequence depends entirely on what reads the file next. Some tools honour the page geometry and render the image back at its original size, transparent margin restored — so the trim looks like it did nothing. Some ignore it and show the trimmed pixels. So the same file appears trimmed in one program and untrimmed in another, which is a miserable thing to debug.

You can see the difference directly:

# Without +repage — note the geometry and the +offset
magick input.png -trim info:
# input.png PNG 262x270 340x340+33+43 8-bit sRGBA

# With +repage — canvas matches the content, no offset
magick input.png -trim +repage info:
# input.png PNG 262x270 262x270+0+0 8-bit sRGBA

-fuzz: the threshold for soft shadows

By default -trim only removes pixels that match the border colour exactly, which for a transparent image means alpha exactly 0. Anything with a drop shadow therefore trims to include the shadow's entire invisible tail — the same limitation Photoshop has.

-fuzz adds tolerance, and it must come before -trim to have any effect:

# Ignore anything within 10% of transparent
magick input.png -fuzz 10% -trim +repage output.png

# More aggressive: cuts well into a soft shadow
magick input.png -fuzz 40% -trim +repage output.png

The percentage is of the full channel range, so -fuzz 10% is roughly alpha 25 of 255 and -fuzz 40% is roughly alpha 102. That maps onto the same trade-off as the threshold control in the browser tool — on a 340 × 340 shadowed icon, tight tolerance gives 262 × 270 and aggressive tolerance gives 216 × 224. The measured table is a reasonable guide for picking a fuzz value.

Useful variations

Trim a whole folder into another directory:

mkdir -p trimmed
mogrify -path trimmed/ -trim +repage *.png

-path matters — without it mogrify overwrites the originals in place, and there is no undo.

Trim, then add a known transparent margin back on all four sides:

magick input.png -trim +repage \
  -bordercolor none -border 24 \
  output.png

Print the bounding box without writing a file:

magick input.png -format "%@" info:
# 262x270+33+43  →  width x height + xOffset + yOffset

Force the border to be treated as transparent rather than a colour:

magick input.png -background none -trim +repage output.png

Trim only some sides — crop to the box, then restore one margin:

# ImageMagick has no per-side trim; compute the box, then crop by hand
magick input.png -format "%@" info:
magick input.png -crop 262x340+33+0 +repage output.png

ImageMagick or the browser?

SituationBetter choice
A build step, CI job, pre-commit hook or nightly pipelineImageMagick. It scripts cleanly and needs no interaction.
Hundreds of files, repeatedly, on your own machineImageMagick with mogrify.
A handful of files, on a machine where you cannot install softwareBrowser batch trimming. Nothing to install, nothing uploaded.
You need to see the result and tune the threshold by eyeThe browser tool — the preview shows the detected box as you move the slider.
Client work under NDA on a borrowed computerEither, as long as it runs locally. Both do.

ImageMagick trimming: questions

What is the ImageMagick command to trim a transparent border?
magick input.png -trim +repage output.png. The -trim flag computes the content bounding box and the +repage flag resets the virtual canvas so the crop is actually applied rather than merely recorded as an offset.
What does +repage do and why does it matter?
ImageMagick keeps a virtual canvas: after -trim the image data is cropped but the file still records the original canvas size and the offset of the crop inside it. Some viewers honour that and show the original dimensions, so the trim appears not to have worked. +repage discards the canvas and offset, making the crop final.
How do I set a threshold for trimming with ImageMagick?
Use -fuzz before -trim, as a percentage: magick in.png -fuzz 10% -trim +repage out.png. On a transparent image the fuzz applies to the alpha comparison, so it behaves like an alpha threshold and lets you cut into a soft shadow.
Why does -trim do nothing on my image?
Either the content already touches all four edges, or the border is not uniform enough. Compression noise and gradients defeat an exact match, so add -fuzz. On a JPG there is no transparency at all, so -trim falls back to matching the corner colour.
How do I trim a whole folder with ImageMagick?
mogrify -path output/ -trim +repage *.png trims every PNG and writes the results to another directory. Always use -path so the originals are not overwritten in place.