What is pixelmatch?
The pixelmatch npm package is a high-performance, zero-dependency library for comparing two images. It is primarily used to find differences between images in terms of pixels, making it highly useful for tasks such as visual regression testing, automated testing of graphics or rendering engines, and anywhere else where image differences need to be quantified programmatically.
What are pixelmatch's main functionalities?
Image comparison
This feature allows you to compare two images and find the number of pixels that differ between them. The `img1` and `img2` parameters are the image data of the two images to compare, `diff` is a Uint8Array or Uint8ClampedArray to store the diff output, `width` and `height` specify the dimensions of the images, and the `threshold` option in the options object allows you to specify the sensitivity of the comparison.
const numDiffPixels = pixelmatch(img1, img2, diff, width, height, {threshold: 0.1});
Customizable comparison
Beyond basic image comparison, pixelmatch allows for customization of the comparison process. The `includeAA` option, for example, can be set to false to exclude anti-aliased pixels from being considered as differences. This is useful for comparing images where anti-aliasing may vary but is not considered a significant difference.
const numDiffPixels = pixelmatch(img1, img2, diff, width, height, {threshold: 0.1, includeAA: false});
Other packages similar to pixelmatch
resemblejs
Resemble.js is a package similar to pixelmatch that analyzes and compares images with a focus on visual differences. It provides more detailed analysis options, such as the ability to ignore colors or to get a percentage of image similarity. However, it might be heavier in terms of dependencies compared to the lightweight nature of pixelmatch.
looks-same
Looks-same is another npm package that offers image comparison capabilities. It focuses on flexibility, offering features like tolerance to color and antialiasing differences. Compared to pixelmatch, looks-same provides a higher-level API that might be more accessible to some users, but potentially at the cost of lower-level control and performance.
pixelmatch
The smallest, simplest and fastest JavaScript pixel-level image comparison library,
primarily designed to be used in regression tests that compare screenshots.
Inspired by Resemble.js and
Blink-diff
and borrows the algorithm from the latter.
Unlike these libraries, pixelmatch is under 80 lines of code,
has no dependencies, and works on raw image data arrays,
so it's blazing fast and can be used in any environment (Node or browsers).
var numMismatchedPixels = pixelmatch(img1.data, img2.data, diff.data, 800, 600);
API
pixelmatch(img1, img2, output, width, height[, threshold, antialiasing])
img1
— image data of the first image (Buffer
or Uint8Array
)img2
— image data of the second imageoutput
— image data to write the diff towidth
— width of the imagesheight
— height of the imagesthreshold
— matching threshold, 0.005
by default, ranges from 0
to 1
antialiasing
— radius of antialiasing to ignore in pixels, 1
by default
Compares two images, writes the output diff and returns the number of mismatched pixels.
Command line
Pixelmatch comes with a binary that works with PNG images:
pixelmatch image1.png image2.png output.png 0.005 1
Install
Install with NPM:
npm install pixelmatch
To build a browser-compatible version, clone the repository locally, then run:
npm install -g browserify
browserify -s pixelmatch index.js > pixelmatch.js
Changelog
1.0.0 (Oct 14, 2015)