How to Batch Crop Images With Imagemagick
Imagemagick is a powerful, command-line based program for manipulating images. In this post I’ll show you how to use Imagemagick to crop several images with one command.
Crop a Single Image
We’ll start by looking at how to crop a single image using Imagemagick. First download Imagemagick if you haven’t already. First, open a command window in the folder containing the image(s) you want to crop.
The command we want to run looks like this:
convert -crop x_sizexy_size+x_offset+y_offset inputfile outputfile
I’ve measured the pixel dimensions in mspaint, so I know exactly the area that I want to crop to. So the command is:
convert -crop 300x200+150+150 CropLayout.png CroppedLayout.png
Which outputs the image ‘CroppedLayout.png’
Batch Crop a Folder of Images
That was the basic usage, but we can also combine it with the Imagemagick batch processor mogrify to crop many images at the same time.
In this example we’ll crop a whole folder full of images to the same dimensions as in the example above.
Here’s what we’re starting with:
First we’ll make a new folder called ‘cropped’ to keep the cropped images in. Now we’ll run the command with a couple of changes:
mogrify -crop 300x300+150+150 -path ./cropped *.png
firstly we’ve specified the output to go to the cropped folder using the ‘-path’ flag. Secondly we’ve told mogrify to look for all .png files in the current folder.