Saturday, January 31, 2015

Cropping a bunch of pictures to the same dimensions

Ah, command line tools, they're so fast. And so easy to use on a Mac.

Given a bunch of image files in the same dimensions, that you want to crop to a fixed portion of the image:

1) Install imagemagick
brew install imagemagick
2) put all the images in a directory by themselves, and cd to that directory in the terminal

3) check the size of one of them using an imagemagick command-line utility:

identify IMG_1400.jpg
IMG_1400.jpg JPEG 960x1280 960x1280+0+0 8-bit sRGB 434KB 0.000u 0:00.000

Oh look, that one has a width of 960 and a height of 1280.

4) crop one of them, look at it, tweak the numbers, repeat until you get the dimensions right:
convert IMG_1400.jpg -crop 750x590+60+320 +repage test.jpg
Convert takes an input file, some processing instructions, and an output file. Here, I'm telling it to crop the image to this geometry (widthxheight+xoffset+yoffset), and then make the output size match what we just cropped it to.

The geometry works like this: move down by the y offset and to the right by the x offset. From this point, keep the portion below and to the right that is as wide as width and as tall as height.

5) Create an output directory.
mkdir output
6) Figure out how to list all your input files. Mine are all named IMG_xxxx.jpg so I can list them like this:
ls IMG_*.jpgIMG_1375.jpg IMG_1380.jpg IMG_1385.jpg...
7) Tell bash to process them all:[1]
for file in `ls IMG*.jpg`
echo $file
convert $file  -crop
750x590+60+320 +repage output/$file
8) Find the results in your output directory, with the same names as the originals.

[1] in one line:
for file in `ls IMG*.jpg`;> do echo $file; convert $file  -crop 7750x590+60+320 +repage out/$file; done

1 comment:

  1. OMG You just made my life when editing!!! Thank you so much for this! I can share with others crop tools guide I found few months ago for mac editor