pixelmatch
A small, simple and fast JavaScript pixel-level image comparison library, originally created to compare screenshots in tests.
Features accurate anti-aliased pixels detection and perceptual color difference metrics. Inspired by Resemble.js and Blink-diff. Unlike these libraries, pixelmatch is just a few hundred lines of code, has no dependencies, and works on raw typed arrays of image data, so it's very fast and can be used in both Node and browsers.
const numDiffPixels = pixelmatch(img1, img2, diff, 800, 600, {threshold: 0.1});
Implements ideas from the following papers:
- A perceptual color space for image processing (2020, BjΓΆrn Ottosson) β the OKLab color space used for color difference.
- Distance metrics for very large color differences (2019, Saeideh Abasi et al.) β the OKLab HyAB metric used to compare colors.
- Anti-aliased pixel and intensity slope detector (2009, Vytautas VyΕ‘niauskas)
Demo
Example output
| expected | actual | diff |
|---|---|---|
API
pixelmatch(img1, img2, output, width, height[, options])
img1,img2β Image data of the images to compare (Buffer,Uint8ArrayorUint8ClampedArray). Note: image dimensions must be equal.outputβ Image data to write the diff to, ornullif don't need a diff image.width,heightβ Width and height of the images. Note that all three images need to have the same dimensions.
options is an object literal with the following properties:
thresholdβ Matching threshold, ranges from0to1. Smaller values make the comparison more sensitive.0.1by default.includeAAβ Iftrue, disables detecting and ignoring anti-aliased pixels.falseby default.alphaβ Blending factor of unchanged pixels in the diff output. Ranges from0for pure white to1for original brightness.0.1by default.aaColorβ The color of anti-aliased pixels in the diff output in[R, G, B]format.[255, 255, 0]by default.diffColorβ The color of differing pixels in the diff output in[R, G, B]format.[255, 0, 0]by default.diffColorAltβ An alternative color to use for dark on light differences to differentiate between "added" and "removed" parts. If not provided, all differing pixels use the color specified bydiffColor.nullby default.diffMaskβ Draw the diff over a transparent background (a mask), rather than over the original image. Will not draw anti-aliased pixels (if detected).checkerboardβ Blend semi-transparent pixels against a checkerboard pattern when comparing (true) rather than plain white (false), avoiding false matches between colors that only look alike over one background.trueby default.windowSizeβ If set to a finite numberN, return the maximum number of differing pixels in anyNΓNsliding window instead of the total count (see below).Infinityby default.
Compares two images, writes the output diff and returns the number of mismatched pixels.
Windowed diff counts
Normally the return value is the total number of differing pixels. With windowSize: N you get the
highest number of diff pixels in any NΓN square instead. Anti-aliased pixels are only included
if includeAA is true.
This helps with noise. GPU dithering and sub-pixel anti-aliasing scatter stray pixels all over the image, so they never fill up one small square, while a real regression usually changes a compact area.
Compare the result against a pixel count: that number is how big a difference you let through, and
N is how closely packed it has to be.
if (pixelmatch(img1, img2, null, width, height, {windowSize: 16}) > 28) throw new Error('changed');
N never exceeds either image dimension, so on a 10Γ2 image windowSize: 32 gives you a 2Γ2
window. The default Infinity makes the window the whole image, which is the total count.
Command line
Pixelmatch comes with a binary that works with PNG images:
pixelmatch image1.png image2.png output.png 0.1
Example usage
Node.js
import fs from 'fs';
import {PNG} from 'pngjs';
import pixelmatch from 'pixelmatch';
const img1 = PNG.sync.read(fs.readFileSync('img1.png'));
const img2 = PNG.sync.read(fs.readFileSync('img2.png'));
const {width, height} = img1;
const diff = new PNG({width, height});
pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1});
fs.writeFileSync('diff.png', PNG.sync.write(diff));
Browsers
const img1 = img1Context.getImageData(0, 0, width, height);
const img2 = img2Context.getImageData(0, 0, width, height);
const diff = diffContext.createImageData(width, height);
pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1});
diffContext.putImageData(diff, 0, 0);
Install
Install with NPM:
npm install pixelmatch
Or use in the browser from a CDN:
<script type="module">
import pixelmatch from 'https://esm.run/pixelmatch';