website-monitor/frontend/remove_bg.js

35 lines
1.1 KiB
JavaScript

const JimpModule = require('jimp');
console.log('Jimp exports:', Object.keys(JimpModule));
const Jimp = JimpModule.Jimp || JimpModule;
const path = require('path');
async function removeBackground(inputPath, outputPath) {
try {
const image = await Jimp.read(inputPath);
image.scan(0, 0, image.bitmap.width, image.bitmap.height, function (x, y, idx) {
const red = this.bitmap.data[idx + 0];
const green = this.bitmap.data[idx + 1];
const blue = this.bitmap.data[idx + 2];
// If white (or close to white), make transparent
if (red > 240 && green > 240 && blue > 240) {
this.bitmap.data[idx + 3] = 0;
}
});
image.write(outputPath, (err) => {
if (err) {
console.error('Error writing file:', err);
} else {
console.log('Successfully removed background');
}
});
} catch (err) {
console.error('Error:', err);
}
}
const logoPath = path.join(__dirname, 'public', 'logo.png');
removeBackground(logoPath, logoPath);