I am trying to extract only white text from the image using pixel manipulation but it does not work on some images. It works on https://prnt.sc/24q29qu but not on https://prnt.sc/24pymne Please see the code used and you may just paste the code in the console to see the output. Tested it on chrome browser. You may click on the output in the console to see the image output.
var imageSource = document.querySelector("body > div.image-constrain.js-image-wrap > div > div > img")
var width = 2*imageSource.width;
var height = 2*imageSource.height;
var img = new Image();
img.crossOrigin = 'anonymous';
img.src = imageSource.src;
var c = document.createElement("canvas")
c.width = width;
c.height = height;
var ctx = c.getContext("2d");
ctx.drawImage(img, 0, 0);
var imageData = ctx.getImageData(0, 0, width, height);
var data = imageData.data;
console.log(data);
// Replacing All the pixels except white
for (let i = 0; i < data.length; i += 4) {
if(data[i] != 255) {
data[i] = 0;
}
if(data[i+1] != 255) {
data[i+1] = 0;
}
if(data[i+2] != 255) {
data[i+2] = 0;
}
if(data[i+3] != 255) {
data[i+3] = 0;
}
}
console.log(imageData.data);
ctx.putImageData(imageData, 0, 0);
img.src = c.toDataURL();
pixelatedImage = c.toDataURL();
console.log(pixelatedImage);