Programmatically resizing a folder of images

This is a note to self about how to programmatically resize and crop many images using ImageMagick.

It all started with a folder full of photos with different pixel sizes and ratios. That is because they had been captured with various cameras and had also been manually cropped. This could be verified by running this command to print their pixel sizes:

identify -format "%wx%h\n" *.JPG

Fortunately, all the images had a reasonably large pixel count, so I decided to go for a 5MP pixel count (2560×1920 in 4:3 ratio). That was achieved with this one-liner:

for i in *.JPG; do convert "$i" -resize 3000x1920 -crop 2560x1920+0+0 "$i"; done

The little script looks for all the image files in the folder and starts by resizing them to the preferred height (1920 pixels) and then cropping them to the correct width (2560 pixels). The result is a folder full of equally sized images.

Ps: the script above overwrites the original files in the folder.

Published by


Alexander Refsum Jensenius is a music researcher and research musician living in Oslo, Norway.