In my recent post A new set of Hubble deep space iMac retina desktops, I included a set of auto-cropped 5120×2880 desktops. In that post, I wrote:
These images were automatically cropped from the master image (after I cropped that; more detail on what I did is coming in a follow-up post), via ImageMagick.
So this would be that post: How to auto-crop huge images using ImageMagick. If you’re not familiar with it, ImageMagick is a set of command-line tools to manipulate images. There are a number of ways to install ImageMagick, but I used Homebrew (brew install imagemagick).
ImageMagick has way too many features to describe here (check the partial list on its site), but the one I was interested in was tile cropping, which subdivides one large image into many smaller images. There are many ways to do auto-cropping, but I just wanted the most basic: For a given input image, create multiple output files using a predefined crop size (5120×2880). That command looks like this:
convert hubble_huge.png -crop 5120x2880 -quality 80% hubble-%d.jpg
Most of this is self-obvious, but the last bit, hubble-%d.jpg, looks a bit odd. This is the output filename, and the %d appends a sequential digit to make each filename unique.
I originally auto-cropped the full Hubble image, which is irregularly shaped. As a result, I got a lot of non-usable images, due to either white boundary lines running through them or due to their position near the right or bottom edge (where it’s not possible to get a full-size crop). I think there were only about ten usable images when I auto-cropped the original image.
To make the auto-crop work better, I first cropped the original image to a true square, as seen here:
(I left the one jutting out bit at the bottom as it’s an isolated cutout, so it only results in two bad images.) This increased the number of usable images to the 27 found in the final post.
If you’ve never used ImageMagick, and are comfortable with the command line, it’s a very useful set of image utilities.