Batch autocropping images the easy way with ImageMagick

I had almost 600 images I wanted to autocrop. It would take about a minute to autocrop each image manually since I would have to repeatedly go through a process of open image, autocrop, export, close. You can imagine how much time it would take to do this manually almost 600 times (about a full working day).

Being the type of person I am I decided I would Google a solution rather than spend hours doing this task manually. Instead of a full working day I did it in 5 minutes, by using an application called ImageMagick.

ImageMagick is mostly a command line based tool which lets you automate processing images in lots of different ways. For example, if you want to build a web application that crops a photo or apply a filter then you might use ImageMagick because it’s easier to interface it with code and is available pre-installed on a large number of web hosting plans.

This is a bit of a power user trick, but if you ever use basic command line functions then this should be relatively simple.

Here’s my solution to overwrite the existing images so they become cropped:

for a in *.jpg; do morgify -trim "$a" "$a"; done

Or if you don’t want to overwrite all the existing images then use this:

for a in *.jpg; do convert -trim "$a" "$a"; done

So if we assume you’re using Linux and you have a directory full of images you want to autocrop in a directory called “images”, then the code to autocrop all the images in the images directory would be:

for a in images/*.jpg; do morgify -trim "$a" "$a"; done

Or if the image format is .png for example then replace .jpg with .png.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.