A TypeScript/JavaScript WebAssembly wrapper for the libimagequant image quantization library. This package provides high-quality image color quantization in the browser using Web Workers for optimal performance.
- High-quality image quantization - Convert 24/32-bit images to 8-bit palette with alpha channel
- Web Worker support - Non-blocking image processing using Web Workers
- TypeScript support - Full TypeScript definitions included
- Promise-based API - Modern, easy-to-use async interface
- Optimized WASM - Size and performance optimized WebAssembly build
- Browser compatible - Works in all modern browsers supporting WebAssembly
- Multiple formats - Supports ESM and CommonJS
npm install libimagequant-wasm- Node.js >= 20.19.0
- Modern browser with WebAssembly and Web Worker support
import LibImageQuant from 'libimagequant-wasm';
const quantizer = new LibImageQuant();
// Fetch a PNG and quantize it
const response = await fetch('image.png');
const pngData = new Uint8Array(await response.arrayBuffer());
const result = await quantizer.quantizePng(pngData, {
maxColors: 64,
quality: { min: 0, target: 100 },
speed: 3,
});
console.log(`Quantized to ${result.paletteLength} colors`);
console.log(`Quality: ${Math.round(result.quality * 100)}%`);
// result.pngBytes - quantized indexed PNG as Uint8Array
// result.imageData - quantized image as ImageData (for canvas rendering)
quantizer.dispose();import LibImageQuant from 'libimagequant-wasm';
const quantizer = new LibImageQuant();
const canvas = document.getElementById('myCanvas') as HTMLCanvasElement;
const ctx = canvas.getContext('2d')!;
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
const result = await quantizer.quantizeImageData(imageData, {
maxColors: 32,
speed: 1,
dithering: 0.8,
});
// Draw quantized result back to canvas
ctx.putImageData(result.imageData, 0, 0);
quantizer.dispose();quantizePng accepts multiple input types:
// Uint8Array
const result = await quantizer.quantizePng(pngBytes);
// ArrayBuffer
const result = await quantizer.quantizePng(arrayBuffer);
// Blob
const result = await quantizer.quantizePng(blob);const quantizer = new LibImageQuant(options?: LibImageQuantOptions);| Option | Type | Default | Description |
|---|---|---|---|
workerUrl |
string |
auto-detected | Custom path to the worker module |
wasmUrl |
string |
auto-detected | Custom path to the WASM module directory |
initTimeout |
number |
10000 |
Worker initialization timeout (ms) |
operationTimeout |
number |
30000 |
Per-operation timeout (ms) |
Quantizes a PNG image from binary data.
pngData:Uint8Array | ArrayBuffer | Blob- PNG file bytesoptions:QuantizationOptions(optional)- Returns:
Promise<QuantizationResult>
Quantizes an ImageData object (e.g. from canvas).
imageData:ImageData- RGBA image dataoptions:QuantizationOptions(optional)- Returns:
Promise<QuantizationResult>
Terminates the worker, rejects any pending operations, and releases resources. Safe to call multiple times.
{
speed?: number; // Speed vs quality (1-10, lower = better quality)
quality?: {
min: number; // Minimum acceptable quality (0-100)
target: number; // Target quality (0-100)
};
maxColors?: number; // Maximum colors in palette (2-256)
dithering?: number; // Dithering level (0.0-1.0)
posterization?: number; // Posterization level (0-4)
}{
palette: number[][]; // Color palette as [[r,g,b,a], ...]
pngBytes: Uint8Array; // Quantized indexed PNG file bytes
imageData: ImageData; // Quantized RGBA image data (for canvas)
quality: number; // Achieved quality (0.0-1.0)
paletteLength: number; // Number of colors in palette
width: number; // Image width in pixels
height: number; // Image height in pixels
}- Rust with the
wasm32-unknown-unknowntarget - wasm-pack
- Node.js >= 20.19.0
- wasm-opt (optional, for WASM size optimization)
# Install Rust and wasm-pack
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
curl https://rustwasm.github.io/wasm-pack/installer/init.sh -sSf | sh
rustup target add wasm32-unknown-unknown
# Install dependencies
npm install
# Build
npm run build| Script | Description |
|---|---|
npm run build |
Full build (WASM + TypeScript + types) |
npm run build:wasm |
Build WASM module only |
npm run build:types |
Generate TypeScript declarations only |
npm run typecheck |
Run TypeScript type checking |
npm test |
Run tests (vitest + browser) |
npm run test:watch |
Run tests in watch mode |
npm run dev |
Start Vite dev server |
npm run clean |
Remove build output |
Tests run in a real browser via Vitest with @vitest/browser and Playwright. No mocks — the actual WASM module is loaded and executed.
# Build first (tests run against built output)
npm run build
# Run tests (headless Chromium)
npm testThe test suite covers:
- High-level API -
LibImageQuantclass, all input formats, options, disposal, concurrency, error handling - Low-level WASM -
ImageQuantizer,QuantizationResult, PNG encode/decode, memory management - Result validation - Color accuracy, PNG integrity, transparency roundtrips
Requires WebAssembly and Web Worker support:
- Chrome 57+
- Firefox 52+
- Safari 11+
- Edge 16+
- Reuse quantizer instances when processing multiple images
- Use appropriate speed settings - higher speed for real-time, lower for final output
- Consider image size - quantization time scales with pixel count
- Call
dispose()when done to release the Web Worker
See examples/test.html for a complete working example with:
- File upload
- Real-time parameter adjustment
- Visual comparison
- Palette display
- Performance metrics
MIT License - See LICENSE file for details.
Important Note: This project wraps the libimagequant library, which is dual-licensed under GPL-3.0 and commercial licenses. While this wrapper is MIT licensed, you must ensure you comply with the appropriate libimagequant license for your use case. For commercial use, you may need to obtain a commercial license from the libimagequant authors.
- Fork the repository
- Create your feature branch
- Build:
npm run build - Test:
npm test - Submit a pull request
Ensure the worker module is served from the same origin, or pass a custom workerUrl to the constructor.
- Verify WebAssembly support:
typeof WebAssembly === 'object' - Check browser console for loading errors
- Ensure proper MIME type for
.wasmfiles (application/wasm) - Pass a custom
wasmUrlif the WASM files are hosted separately
- Large images may take significant time to quantize
- Consider resizing images before quantization
- Use higher
speedsettings for real-time applications