js 转换image data 成黑白色。

We can even make it pure black and white by using the following calculations:

  • black = 0 - 382
  • white = 383 - 765
    And it will result in the following loop:
for (i = 0; i < imgData.data.length; i += 4) {
    let count = imgData.data[i] + imgData.data[i + 1] + imgData.data[i + 2];
    let colour = 0;
    if (count > 383) colour = 255;

    imgData.data[i] = colour;
    imgData.data[i + 1] = colour;
    imgData.data[i + 2] = colour;
    imgData.data[i + 3] = 255;
}
posted @ 2023-05-26 15:45  Regina_wisdom  阅读(93)  评论(0编辑  收藏  举报