wasm-image-to-black-white

Convert an image to black&white (various algorithms)


Install
npm install wasm-image-to-black-white@0.4.2

Documentation

Description

Implementation of various algorithms that transform colourful images to black and white.

Install

npm i wasm-image-to-black-white --save

Algorithms

Original Image

Averaging Color Channels

Gray = (Red + Green + Blue) / 3

const wasm = import("wasm-image-to-black-white");
wasm.then(bnw => {
  img.src = bnw.grayscale_with_luminocity(img);
});

Luminocity

Gray = (Red * 0.21 + Green * 0.72 + Blue * 0.07)

const wasm = import("wasm-image-to-black-white");
wasm.then(bnw => {
  img.src = bnw.grayscale_with_average(img);
});

Desaturation

Gray = ( Max(Red, Green, Blue) + Min(Red, Green, Blue) ) / 2)

const wasm = import("wasm-image-to-black-white");
wasm.then(bnw => {
  img.src = bnw.grayscale_with_desaturation(img);
});        

BT601

Gray = (Red * 0.299 + Green * 0.587 + Blue * 0.114)

const wasm = import("wasm-image-to-black-white");
wasm.then(bnw => {
  img.src = bnw.grayscale_with_BT601(img);
});            

Example

const wasm = import("wasm-image-to-black-white");

const fileUploader = document.querySelector("#uploadfile");

fileUploader.addEventListener('change', (event) => {
  const file = event.target.files[0];

  var img = new Image;

  img.onload = function() {
    wasm.then(bnw => {
      bnw.grayscale_with_luminocity(img);
      bnw.grayscale_with_average(img);
    });
  }

  img.src = URL.createObjectURL(file);
})