Cropping an image programmatically

Imagemagick is an image manipulation library that supports the ability to crop an image programmatically. There is a creative aspect to making a crop from an original image. A photo producer may be unhappy with a crop that was chosen automatically. It may make sense to build a tool that provides a nice front-end crop selection experience with imagemagick performing the actual crop on the server.

This article will assume that having a computer program make a crop from an image is OK. The article gives a little bit of technical detail on how to make a cropped image with imagemagick.

Imagemagick requires a geometry argument for a crop that is comprised of size (width and height) and offset (x and y coordinates). By default the offset is the top left corner of the image. The offset can be changed with the gravity option.

Let's say you wanted to take a wide image and make a 4:5 vertical crop of it. A crop could be taken from the left, center, or right side.

This image is a photo I took of some houses in Old Town Alexandria, Virginia. The photo will serve as the test image for this example, used in the commands below as houses.jpg. The original image size is 640px wide by 331px tall.

To achieve the new vertical orientation with a 4:5 aspect ratio, with a maximum vertical limit of 331 pixels, and in order to retain the full resolution of the image, we could take a 240x300 slice from somewhere in the image. 240x300 is a 4:5 aspect ratio.

On OS X, after installing imagemagick with homebrew (brew install imagemagick), running the following command will produce a new file with a crop from the left.

convert houses.jpg -crop "240x300+0+0" houses-left.jpg

Instead of the left crop, a crop from the center may be more representative of the original image. For a center crop use the gravity option with a value of Center.

convert houses.jpg -gravity Center -crop "240x300+0+0" houses-center.jpg

In the center crop output file, we can see the house on the right is not visible at all. A final center crop might move the crop area to the right to get some of the detail from the house on the right, as well as move the crop area down to get more house detail and less sky. The offset is a x and y coordinate pair, so from center, move it over 25 and down 31.

convert houses.jpg -gravity Center -crop "240x300+25+31" houses-center-final.jpg

The final image is a vertical crop of what was originally a wide aspect ratio, that retains much of the subject matter from the original picture. The final crop favors detail from the houses over the sky, by shifting the viewport down and showing a bit of each house on either side of the center house.