Published on

Cropping / Trimming the Border Around an Image with Transparency using ImageMagick


Today I was busy with some UI changes.

The one issue I ran into was that the images the designer sent me had padding in the image - this made it difficult to properly space everything else around these images. In situations like this, I generally turn to ImageMagick, especially when dealing with more than 1 image. ImageMagick is like the Swiss Army Knife of command-line image manipulation.

After a bit of fiddling and searching I came upon this post which suggested something like this which worked perfectly:

mogrify -trim +repage -alpha Background image.png

This works even in situations where your image has transparency.

I put this into a for loop and resolved all these padding issues:

for file in *.png;do echo $file;mogrify -trim +repage -alpha Background $file;done