fabric js or imagick remove white from image

不羁岁月 提交于 2019-12-06 15:54:13

I got it to work. By using factors from the YUV color space, I could create a Fabric JS image filter.

It was a lot of Trial-and-error to get the result I was looking for. Therefore I don't have a (detailed) description of how this works. For all I know is that I have used the YUV factors to get the brightness of the (RGB) colours.

for (i = 0; i < iLen; i++) {
    for (j = 0; j < jLen; j++) {
      index = (i * 4) * jLen + (j * 4);
      var yuv = {};
      yuv.r = data[index] / 255 * 1 * 0.299;     
      yuv.g = data[index + 1] / 255 * 1 * 0.587; //we use the yuv formula constants
      yuv.b = data[index + 2] / 255 * 1 * 0.114; //to try to catch the Y (brightness)
      yuv.y = yuv.r + yuv.g + yuv.b;             //you can tweak this
      data[index + 3] = 350 - (yuv.y / 1 * 255); //by changing the first number after the "=" on this line!
    }
}

Somehow by messing with the 350 on the last line you can alter the transparency factor.

Sorry for not being able to explain more about how this Fabric filter works.

So there is a filter in Fabric.js that does just that.

http://fabricjs.com/docs/fabric.Image.filters.RemoveWhite.html

var filter = new fabric.Image.filters.RemoveWhite({
  threshold: 40,
  distance: 140
});
image.filters.push(filter);
image.applyFilters(canvas.renderAll.bind(canvas));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!