Open madskunker opened 5 years ago
I'm having the same issue with this image:
const jimp = require('jimp');
async function main() {
const image = await jimp.read('test/image.png');
image.autocrop(0.6)
.write('test/result.png'); // save
}
main();
Yeah, this was broken. I tried to debug it, I almost thought that getting RGBA color information was wrong, but I think somehow the X,Y values in the loops are out of whack with what it's supposed to be getting. Not a very deep look on my part, but needed this to work so I rewrote the code pretty simply (and I only needed to identify where transparent pixels need to crop to in my use case). Here's my code if it helps anyone:
module.exports = function autocrop() { var w = this.bitmap.width; var h = this.bitmap.height; const l = findLeftSide(this, w, h); const r = findRightSide(this, w, h); const t = findTopSide(this, w, h); const b = findBottomSide(this, w, h); this.crop(l, t, w - (w - r + l), h - (h - b + t)); return this; };
function findLeftSide(scope, w, h) { for (let x = 0; x < w; x++) { for ( let y = 0; y < h; y++) { var clr = scope.getPixelColor(x, y); var rgba = scope.constructor.intToRGBA(clr);
if (rgba.a !== 0) {
return x;
}
}
}
}
function findRightSide(scope, w, h) { for (let x = w; x > 0; x--) { for ( let y = 0; y < h; y++) { var clr = scope.getPixelColor(x, y); var rgba = scope.constructor.intToRGBA(clr);
if (rgba.a !== 0) {
return x;
}
}
}
}
function findTopSide(scope, w, h) { for ( let y = 0; y < h; y++) { for (let x = 0; x < w; x++) { var clr = scope.getPixelColor(x, y); var rgba = scope.constructor.intToRGBA(clr);
if (rgba.a !== 0) {
return y;
}
}
}
}
function findBottomSide(scope, w, h) { for ( let y = h; y > 0; y--) { for (let x = 0; x < w; x++) { var clr = scope.getPixelColor(x, y); var rgba = scope.constructor.intToRGBA(clr);
if (rgba.a !== 0) {
return y;
}
}
}
}
And then to use this (because I hadn't had a chance to actually look at how to make plugins), I simply set the loaded image object's autocrop function to this:
const autocrop = require('theabovemodule.js'); myloadedimage.autocrop = autocrop;
Anyway, it works for me as a hacked together solution until this bug is fixed
any updates ?
Large Black borders should have cropped out. I have adjusted the tolerance and have see no change in results. They are simply not getting cropped.
Latest Node and Jimp on a Raspberry Pi