cake
(content-aware kropping [sic] by entropy) is a
greedy image cropper inspired by Reddit's scraper library. It works
by moving a sliding window over a given image that is continually
reduced to the size of a specified crop width and height. Two copies of
the current window boundary are made, and for each, 16px are cropped on
opposite sides (left-right on the first pass and top-bottom on the
second). The entropies of the cropped windows are compared, and the
smaller of the two will be discarded, moving the window toward the
larger.
The current "smart" implementation takes the smaller dimension of the input image as the crop width and height. The optimal crop position is found, and the image is then scaled to 100x100 px.
Make sure that you have chunky_png
installed, and you're good to go.
This has also only been tested on Ruby 1.9.3p0.
Usage: cake.rb [options] <input ...>
-o, --output <string> Specify output file
-s, --smart Selects the largest, optimal square to crop and rescales. Good for thumbnails.
--debug Draws the bounding crop area instead of cropping the image
-d <integer>x<integer>, Specify output dimensions (width x height) in pixels
--dimensions
$ ruby cake.rb -s cat.png
$ ruby cake.rb --debug -d 240x240 soundlab.png
Because of the large number of colors in an image, it would probably be more ideal to do color quantization before processing.
@mikewoodhouse, I do the resize because the problem says not to build the solution around the sample images (or maybe it meant not to hardcode crop areas). But if I had a 1600x1600 image, a 100x100 crop would be rather useless.
My solution does support cropping without resizing (actually, by default, when you don't specify the
--smart
option), but I found what I submitted to produce better results. Here are the default outputs without resizing.cat.png: output
dog.png: output
duck.png: output