I am creating an Electron app and I have buffered data from an image in the form:

[137, 80, 78, 71, 13, 10, 26, 10, 0, 0, 0, 13, 73, 72, 68, 82, 0, 0, 2, 88, 0, 0, 0, 120, 8, 2, 0, 0, 0, 161, 55, 27, 148, 0, 0, 0, 9, 112, 72, 89, 115, 0, 0, 11, 18, 0, 0, 11, 18, 1, 210, 221, 126, 252, 0, 0, 32, 0, 73, 68, 65, 84, 120, 218, 236, 189, 7, 152, 84, 69, 214, 255, 191, 207, 255, 217, 223, 238, 42, 97, 102, 58, 231, 112, 67, 231, 137, 12, 57, 231, 201, 211, 211, 211, 57, 119, 79, 102, 72, 2, …]

Which I need to convert to black and white but whenever I run it through the binarize function below, I get errors like TypeError: Cannot read property 'mime' of null.

I'm using this code to convert to black and white:

function binarize(data) {
  var RED_INTENCITY_COEF = 0.2126;
  var GREEN_INTENCITY_COEF = 0.7152;
  var BLUE_INTENCITY_COEF = 0.0722;

  for(var i = 0; i < data.length; i += 4) {
    var brightness = RED_INTENCITY_COEF * data[i] + GREEN_INTENCITY_COEF * data[i + 1] + BLUE_INTENCITY_COEF * data[i + 2];
    // red
    data[i] = brightness;
    // green
    data[i + 1] = brightness;
    // blue
    data[i + 2] = brightness;
  }
  return data;
}

data = binarize(data)

The image is in buffer only and does not need to be displayed anywhere. The image is fetched from a png file.

0 Answers