jimp-smartcrop
is a fork of SmartCrop.js to run on
Node.js using Jimp and without any native dependencies.
It implements an algorithm to find good crops for images.
This library also contains a face detection algorithm based on ccv-purejs. However, at this point the smart cropping and face detection is not connected to each other but separate functionality.
THIS LIBARARY IS PROVIDED AS IS WITHOUT ANY INTENT TO FURTHER MAINTAIN FOR NOW. I've written this as I planned to use it, but things turned out differently. However, I leave this here in case you find it useful. In my eyes the best would be to rewrite the smart crop and face detection to become plugins for JIMP itself.
Image: https://www.flickr.com/photos/endogamia/5682480447/ by N. Feans
- Test Suite, contains over 100 images, heavy
- Test Bed, allows you to upload your own images
- Photo transitions, automatically creates Ken Burns transitions for a slide show.
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
- 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
SmartCrop.crop(image, {width: 100, height: 100}, function(result){console.log(result);});
// {topCrop: {x: 300, y: 200, height: 200, width: 200}}
npm install node-smartcrop
The API is not yet finalized. Look at the code for details and expect changes.
Crop image using options and call callback(result) when done.
image: A Jimp image.
options: see cropOptions
callback: function(cropResult)
debug: if true, cropResults will contain a debugCanvas
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.
There are many more (for now undocumented) options available. Check the source and know that they might change in the future.
{
topCrop: crop,
crops: [crop]
}
{
x: 1,
y: 1,
width: 1,
height: 1
}
You can run the tests running npm test
.
The test coverage for smartcrop.js is very limited at the moment. I expect to improve this as the code matures and the concepts solidify.
TODO
- smartcrop.js the original implementation
- connect-thumbs Middleware for connect.js that supports smartcrop.js by Irakli Nadareishvili
- smartcrop.go by Christian Muehlhaeuser
- smartcrop.py by Hideo Hattori
- smartcrop-rails smartcrop wrapped in a ruby gem by Mohammed Sadiq
Original source code is copyright (c) 2014 Jonas Wanger, licensed under the MIT License (enclosed)