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.
This really annoying - it uses entropy (in a way that I wanted to but couldn't figure out) but then crops to a proportion and then resizes, which isn't how I read the challenge. So plus point for the math, minus point for the resize.