Replies: 1 comment
|
For a solid white background, you can make matching pixels transparent by changing their alpha channel, then write a PNG: import { Jimp } from 'jimp';
function removeWhite(image, threshold = 255) {
const data = image.bitmap.data;
image.scan((x, y, index) => {
if (data[index] >= threshold &&
data[index + 1] >= threshold &&
data[index + 2] >= threshold) {
data[index + 3] = 0;
}
});
return image;
}
const image = await Jimp.read('input.png');
removeWhite(image);
await image.write('transparent.png');The default removes exactly white pixels; a value such as Use PNG for the result because JPEG does not retain alpha transparency. Tested with Jimp 1.6.1: exact-white alpha became zero, colored pixels stayed unchanged, the threshold option handled near-white pixels, and transparency survived PNG write/read. Jimp bitmap/pixel API. |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Say it's a white background, how would you remove that and make it transparent?
All reactions