Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 1,490 Bytes
cb3fdda |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
export function replaceNonWhiteWithTransparent(imageBase64: string): Promise<string> {
return new Promise((resolve, reject) => {
const img = new Image();
img.onload = () => {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
if (!ctx) {
reject('Unable to get canvas context');
return;
}
const ratio = window.devicePixelRatio || 1;
canvas.width = img.width * ratio;
canvas.height = img.height * ratio;
ctx.scale(ratio, ratio);
ctx.drawImage(img, 0, 0);
const imageData = ctx.getImageData(0, 0, img.width, img.height);
const data = imageData.data;
console.log("ok")
for (let i = 0; i < data.length; i += 4) {
if (data[i] === 255 && data[i + 1] === 255 && data[i + 2] === 255) {
// Change white (also shades of grays) pixels to black
data[i] = 0;
data[i + 1] = 0;
data[i + 2] = 0;
} else {
// Change all other pixels to transparent
data[i + 3] = 0;
}
}
ctx.putImageData(imageData, 0, 0);
resolve(canvas.toDataURL());
};
img.onerror = (err) => {
reject(err);
};
img.src = imageBase64;
});
} |