Smartcrop.js implements an algorithm to find good crops for images. It can be used in the browser, in node or via a CLI.
Image: https://www.flickr.com/photos/endogamia/5682480447/ by N. Feans
- Smartcrop.js Test Suite, contains over 100 images, heavy.
- Smartcrop.js Test Bed, allows you to test smartcrop with your own images and different face detection libraries.
- Automatic Photo transitions, automatically creates Ken Burns transitions for a slide show.
// you pass in an image as well as the width & height of the crop you// want to optimize.smartcrop;
// smartcrop will output you it's best guess for a crop// you can now use this data to crop the image.topCrop: x: 300 y: 200 height: 200 width: 200
npm install smartcrop
or just download smartcrop.js from the git repository.
Command Line Interface
The smartcrop-cli offers command line interface to smartcrop.js.
You can use smartcrop from nodejs via either smartcrop-gm (which is using image magick via gm) or smartcrop-sharp (which is using libvips via sharp). The smartcrop-cli can be used as an example of using smartcrop from node.
While smartcrop.js is a small personal project it is currently being used on high traffic production sites. It has a basic set of automated tests and a test coverage of close to 100%. The tests are ran in all modern browsers thanks to saucelabs. If in any doubt the code is short enough to perform a quick review yourself.
Smartcrop.js works using fairly dumb image processing. In short:
- Find edges using laplace
- Find regions with a color like skin
- Find regions high in saturation
- Boost regions as specified by options (for example detected faces)
- Generate a set of candidate crops using a sliding window
- Rank them using an importance function to focus the detail in the center and avoid it in the edges.
- Output the candidate crop with the highest rank
The smartcrop algorithm itself is designed to be simple, relatively fast, small and generic.
In many cases it does make sense to add face detection to it to ensure faces get the priority they deserve.
You can experiment with all of these in the smartcrop.js testbed
On the server side node-opencv can be quicker but comes with some annoying issues as well.
Supported Module Formats
- global export / window
Find the best crop for image using options.
image: anything ctx.drawImage() accepts, usually HTMLImageElement, HTMLCanvasElement or HTMLVideoElement.
returns: A promise for a cropResult.
minScale: minimal scale of the crop rect, set to 1.0 to prevent smaller than necessary crops (lowers the risk of chopping things off).
width: width of the crop you want to use.
height: height of the crop you want to use.
boost: optional array of regions whose 'interestingness' you want to boost (for example faces). See boost;
ruleOfThirds: optional boolean if set to false it will turn off the rule of thirds composition weight.
debug (internal): if true, cropResults will contain a debugCanvas and the complete results array.
There are many more (for now undocumented) options available. Check the source and be advised that they might change in the future.
Result of the promise returned by smartcrop.crop.
An individual crop.
x: 11 // pixels from the left sidey: 20 // pixels from the topwidth: 1 // pixelsheight: 1 // pixels
Describes a region to boost. A usage example of this is to take into account faces in the image. See smartcrop-cli for an example on how to integrate face detection.
x: 11 // pixels from the left sidey: 20 // pixels from the topwidth: 1 // pixelsheight: 1 // pixelsweight: 1 // [0, 1]
You can run the tests using
grunt test. Alternatively you can also just run grunt (the default task) and open http://localhost:8000/test/.
There are benchmarks for both the browser (test/benchmark.html) and node (node test/benchmark-node.js [requires node-canvas]) both powered by benchmark.js.
If you just want some rough numbers: It takes < 20 ms to find a square crop of a 640x427px picture on an i7. In other words, it's fine to run it on one image, it's suboptimal to run it on an entire gallery on page load.
- connect-thumbs Middleware for connect.js that supports smartcrop.js by Irakli Nadareishvili
- smartcrop-java by QuadFlask
- smartcrop-android by QuadFlask
- smartcrop.go by Christian Muehlhaeuser
- smartcrop.py by Hideo Hattori
- smartcrop-rails smartcrop wrapped in a ruby gem by Mohammed Sadiq
In short: It's a lot faster when calculating bigger crops. The quality of the crops should be comparable but the results are going to be different so this will be a major release.
Removed useless files from npm package.
Creating github releases. Added options.input which is getting passed along to iop.open.
Refactoring/cleanup to make it easier to use with node.js (dropping the node-canvas dependency) and enable support for boosts which can be used to do face detection. This is a 1.0 in the semantic meaning (denoting backwards incompatible API changes). It does not denote a finished product.
Copyright (c) 2018 Jonas Wagner, licensed under the MIT License (enclosed)