Skip to content
This repository has been archived by the owner on Jun 21, 2022. It is now read-only.

arabold/node-smartcrop

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jimp-smartcrop

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.

Example Image: https://www.flickr.com/photos/endogamia/5682480447/ by N. Feans

Demos

Algorithm Overview

Smartcrop.js works using fairly dumb image processing. In short:

  1. Find edges using laplace
  2. Find regions with a color like skin
  3. Find regions high in saturation
  4. Generate a set of candidate crops using a sliding window
  5. Rank them using an importance function to focus the detail in the center and avoid it in the edges.
  6. Output the candidate crop with the highest rank

Simple Example

SmartCrop.crop(image, {width: 100, height: 100}, function(result){console.log(result);});
// {topCrop: {x: 300, y: 200, height: 200, width: 200}}

Download/ Installation

npm install node-smartcrop

API

The API is not yet finalized. Look at the code for details and expect changes.

SmartCrop.crop(image, options, callback)

Crop image using options and call callback(result) when done.

image: A Jimp image.

options: see cropOptions

callback: function(cropResult)

cropOptions

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.

cropResult

{
  topCrop: crop,
  crops: [crop]
}

crop

{
  x: 1,
  y: 1,
  width: 1,
  height: 1
}

Tests

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.

Benchmark

TODO

Contributors

Ports, Alternatives

License

Original source code is copyright (c) 2014 Jonas Wanger, licensed under the MIT License (enclosed)

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%