node.js port of jueseph/Opticrop
Detect the most interesting part of picture and crops it
tested on node v7.7.4
Unlike most cropping routines out there, Opticrop uses edge-detection to find the most “interesting” part of the image to crop, so you won’t get a useless thumbnail just because the top-left corner of your image happened to be a big patch of featureless sky.
Opticrop was written to be smarter than easily available alternatives, but because the strategy is relatively simple, it still makes the “wrong” crop on certain images.
Example #1 - crops original image just fine
Example #2 - the result is perfect
Example #3 - result is not as good as you probably expected
First install graphicsmagick and dev version od GD2 (more info here)
sudo apt-get install graphicsmagick
sudo apt-get install libgd2-xpm-dev
OR
sudo apt-get install libgd2-dev # libgd
npm install opticrop-node
$ sudo yum install gd-devel
npm install opticrop-node
brew install graphicsmagick
brew install pkg-config gd
npm install opticrop-node
Will not build on Windows!
cropTo returns bluebird Promise if callback
param is not passed down
const Opticrop = require('opticrop-node').Opticrop;
const opticrop = new Opticrop;
opticrop
.setImage(inFile)
.setWidth(100)
.setHeight(100)
.cropTo(outFile)
.then(() => {
console.log('crop is done')
})
.catch((err) => {
console.log("Error", err);
});
or it accepts a callback as last argument
const Opticrop = require('opticrop-node').Opticrop;
const opticrop = new Opticrop;
opticrop
.setImage('./images/example.jpg')
.setWidth(100)
.setHeight(100)
.cropTo('./images/cropped_example.jpg', (err, data) => {
console.log("Cropping done", err, data);
});
Following command will crop example images and save as images/cropped_example.(gif|jpg|png)
node test.js
You also can check if there are memory leaks
TEST_MEMORY=1 node test.js
Rewritten in TypeScript
Cropping speed was increased by 3 times due to replacing of mikolalysenko/get-pixels with mikesmullin/node-gd
Ported version of jueseph/Opticrop