Greyscale dithering for the Node.js
npm install floyd-steinberg
The Floyd-Steinberg dithering algorithm was published by Robert Floyd and Louis Steinberg in 1976. Dithering is a method of diffusing pixels in order to avoid harsh edges or banding where the colours in an image contrast with each other. Its obvious use is in converting high depth images to a limited colour palette (256 or less). There are many dithering algorithms out there, and Floyd-Steinberg is one of the most well known.
How do I use this module?
This module expects a PNG format image object to be passed in as a single argument. This image object must comply with the same format as the HTML5 canvas ImageData spec (see https://developer.mozilla.org/en-US/docs/Web/API/ImageData). There are several node packages out there that can parse/decode PNG files into this format. Try pngparse or pngjs from the npmjs.com repository.
var floydSteinberg = ;var ditherImage = ;console;
And the result:
ditherImage =height: intwidth: intdata: ...
From there, you may pipe this into a PNG file output solution, or use the object to manipulate/use in your project. "What is all this piping business?" - I'm glad you asked. Read this streams quick guide, and in-depth streams adventure.
Example converting an image from the filesystem using pngjs:
var floydSteinberg = ;var fs = ;var PNG = PNG;fs;
How does it look?
Current file output of this module: