What is image-q?
image-q is an advanced image quantization library for Node.js and the browser. It provides various algorithms for color quantization and dithering, allowing you to reduce the number of colors in an image while maintaining visual quality.
What are image-q's main functionalities?
Color Quantization
This feature allows you to reduce the number of colors in an image using various quantization algorithms. The code sample demonstrates how to load an image, create a quantizer instance, and quantize the image.
const { ImageQuantizer, Palette } = require('image-q');
const fs = require('fs');
// Load an image (example uses a buffer from a file)
const imageBuffer = fs.readFileSync('path/to/image.png');
// Create a quantizer instance
const quantizer = new ImageQuantizer();
// Quantize the image
quantizer.quantize(imageBuffer).then((result) => {
// Save the quantized image
fs.writeFileSync('path/to/quantized_image.png', result);
});
Dithering
This feature allows you to apply dithering to an image, which helps to reduce color banding and improve visual quality. The code sample demonstrates how to load an image, create a ditherer instance, and apply dithering to the image.
const { ImageDitherer } = require('image-q');
const fs = require('fs');
// Load an image (example uses a buffer from a file)
const imageBuffer = fs.readFileSync('path/to/image.png');
// Create a ditherer instance
const ditherer = new ImageDitherer();
// Apply dithering to the image
const ditheredImage = ditherer.dither(imageBuffer);
// Save the dithered image
fs.writeFileSync('path/to/dithered_image.png', ditheredImage);
Palette Extraction
This feature allows you to extract the color palette from an image. The code sample demonstrates how to load an image, create a palette instance, and extract the palette colors from the image.
const { Palette } = require('image-q');
const fs = require('fs');
// Load an image (example uses a buffer from a file)
const imageBuffer = fs.readFileSync('path/to/image.png');
// Create a palette instance
const palette = new Palette();
// Extract the palette from the image
palette.buildPalette(imageBuffer).then((paletteColors) => {
console.log('Extracted Palette:', paletteColors);
});
Other packages similar to image-q
quantize
quantize is a library for color quantization using the MMCQ (Modified Median Cut Quantization) algorithm. It is lightweight and easy to use for reducing the number of colors in an image. However, it lacks the advanced dithering and palette extraction features provided by image-q.
color-thief
color-thief is a library for extracting the dominant color or a palette of colors from an image. It is designed to be simple and efficient, but it does not offer the same level of customization and advanced features as image-q, such as dithering and multiple quantization algorithms.
IQ.ts
Complete Image Quantization Library in TypeScript (MIT License)


Table of Contents
Introduction
Image Color Number Reduction with alpha support using RgbQuant/NeuQuant/Xiaolin Wu's algorithms and Euclidean/Manhattan/CIEDE2000 color distance formulas in TypeScript
Capability
-
Platforms supported
- browser (Chrome 7.0+, FireFox 4.0+, IE 10+, Opera 11.6+, Safari 5.1+)
- node.js (Node.js 0.9.0+)
-
Builds
- iq.js - UMD build (
import * as iq from "image-q"
)
-
Import
HTMLImageElement
HTMLCanvasElement
NodeCanvas
ImageData
Array
CanvasPixelArray
Uint8Array
Uint32Array
-
Color Distance
Euclidean
- 1/1/1/1 coefficients (originally used in Xiaolin Wu's Quantizer WuQuant)
EuclideanRgbQuantWOAlpha
- BT.709 sRGB coefficients (originally used in RgbQuant)
EuclideanRgbQuantWithAlpha
BT.709 sRGB coefficients + alpha support
Manhattan
- 1/1/1/1 coefficients (originally used in NeuQuant)
ManhattanSRGB
- BT.709 sRGB coefficients
ManhattanNommyde
- see https://github.com/igor-bezkrovny/image-quantization/issues/4#issuecomment-234527620
CIEDE2000
- CIEDE2000 (very slow)
CIE94Textiles
- CIE94 implementation for textiles
CIE94GraphicArts
- CIE94 implementation for graphic arts
CMETRIC
- see http://www.compuphase.com/cmetric.htm
PNGQUANT
- used in pngQuant tool
-
Palette Quantizers
NeuQuant
(original code ported, integer calculations)
NeuQuantFloat
(floating-point calculations)
RgbQuant
WuQuant
-
Image Quantizers
NearestColor
ErrorDiffusionArray
- two modes of error propagation are supported: xnview
and gimp
FloydSteinberg
FalseFloydSteinberg
Stucki
Atkinson
Jarvis
Burkes
Sierra
TwoSierra
SierraLite
ErrorDiffusionRiemersma
- Hilbert space-filling curve is used
-
Output
Include IQ Library into your project
ES6 module
import * as iq from "image-q"
CommonJS
var iq = require("image-q");
As a global variable (Browser)
<script src="<path-to image-q/dist/iq.js>" type="text/javascript" charset="utf-8"></script>
Usage
Load Image (simple example)
var img = document.createElement("img");
img.onload = function() {
...
}
img.src = "http://pixabay.com/static/uploads/photo/2012/04/11/11/32/letter-a-27580_640.png"
Generate Palette
var targetColors = 256;
var pointContainer = iq.utils.PointContainer.fromHTMLImageElement(img);
var distanceCalculator = new iq.distance.Euclidean();
var paletteQuantizer = new iq.palette.RgbQuant(distanceCalculator, targetColors);
paletteQuantizer.sample(pointContainer);
... (you may sample more than one image to create mutual palette)
var palette = paletteQuantizer.quantize();
Apply Palette to Image (Image Dithering)
var imageDitherer = new iq.image.NearestColor(distanceCalculator);
var resultPointContainer = imageQuantizer.quantize(pointContainer, palette);
You may work with resultPointContainer directly or you may convert it to Uint8Array
/Uint32Array
var uint8array = resultPointContainer.toUint8Array();
TODO
- notification about progress
riemersma dithering
- ordered dithering
Changelog
1.1.1 (2016-08-28)
+ CIEDE2000 - incorrect calculation fixed
+ CIEDE2000 - alpha channel now has only 25% impact on color distance instead of 66%
+ CIE94 - added 2 types (textiles and graphics art) according to spec
+ CIE94 - alpha support added
+ rgb2xyz, lab2xyz, xyz2rgb, xyz2lab - gamma correction
+ lab2xyz, xyz2lab - refY should be 100 (1.00000) instead of 10 (0.10000)
+ manhattan with new (Nommyde) coefficients added
+ mocha tests added
+ webpack integration
+ image-q is now UMD module
+ travis-ci integration
+ typescript 2.0
+ indentation with 4 spaces
0.1.4 (2015-06-24)
+ Refactoring
+ Riemersma dithering added (Hilbert Curve)
+ Readme.md updated
+ build.cmd updated
0.1.3 (2015-06-16)
+ NeuQuant is fixed (again) according to original Anthony Dekker source code (all values should be integer)
+ Error Diffusion Dithering is now calculates error like XNVIEW
+ Refactoring
0.1.2 (2015-06-16)
+ Documentation generation fixed
+ File name case problem fixed
0.1.1 (2015-06-16)
+ Auto-generated documentation added
+ Refactoring
0.1.0 (2015-06-16)
+ Code cleanup, removed unnecessary files
0.0.5 (2015-06-16)
+ PNGQUANT color distance added, need to check its quality
+ CIEDE2000 and CIE94 fixed for use in NeuQuant
+ NeuQuant is fixed according to original Anthony Dekker source code (all values should be integer)
+ Code refactoring and cleanup
* We have some slowdown because of red/green/blue/alpha normalization according to white point per each calculateRaw/calculateNormalized call
0.0.4 (2015-06-15)
+ CIEDE2000 color distance equation optimized (original CIEDE2000 equation is available as class `CIEDE2000_Original`)
0.0.3b (2015-06-11)
+ CMETRIC color distance fixed
0.0.3a (2015-06-11)
+ Cleanup
+ Draft of CMETRIC color distance added
0.0.2 (2015-06-10)
+ rgb2xyz & xyz2lab fixed. CIEDE2000 works much better now.
+ CIE94 distance formula added. More investigation is needed.
0.0.1
+ Initial
Credits
Thanks to Leon Sorokin for information share and his original RgbQuant!
References
-
Palette Quantization Algorithms
-
Image Quantization Algorithms
-
Color Distance Formulas
Calculator + Info
- Euclidean Distance
- Manhattan Distance
- CIE94 Distance
- CIEDE2000
- Euclidean Distance w/o Alpha (RgbQuant)
- Euclidean Distance w/o sRGB coefficients (Xiaolin Wu Quant)
- Manhattan Distance w/o sRGB coefficients (NeuQuant)
- CMETRIC
DRAFT!
-
Color conversion formulas
Be sure to fix rgb2xyz/xyz2lab. Issue is with strange part of code: r = r > 0.04045 ? ...
. Check http://en.wikipedia.org/wiki/Lab_color_space
-
Image Quality Assessment
-
Other
License
MIT