Features accurate anti-aliased pixels detection and perceptual color difference metrics.
Inspired by Resemble.js and Blink-diff. Unlike these libraries, pixelmatch is around 150 lines of code, has no dependencies, and works on raw typed arrays of image data, so it's blazing fast and can be used in any environment (Node or browsers).
const numDiffPixels = ;
Implements ideas from the following papers:
- Measuring perceived color difference using YIQ NTSC transmission color space in mobile applications (2010, Yuriy Kotsarenko, Fernando Ramos)
- Anti-aliased pixel and intensity slope detector (2009, Vytautas Vyšniauskas)
pixelmatch(img1, img2, output, width, height[, options])
img2— Image data of the images to compare (
Uint8ClampedArray). Note: image dimensions must be equal.
output— Image data to write the diff to, or
nullif don't need a diff image.
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 from
1. Smaller values make the comparison more sensitive.
true, disables detecting and ignoring anti-aliased pixels.
alpha— Blending factor of unchanged pixels in the diff output. Ranges from
0for pure white to
1for original brightness.
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 by
diffMask— Draw the diff over a transparent background (a mask), rather than over the original image. Will not draw anti-aliased pixels (if detected).
Compares two images, writes the output diff and returns the number of mismatched pixels.
Pixelmatch comes with a binary that works with PNG images:
pixelmatch image1.png image2.png output.png 0.1
const fs = ;const PNG = PNG;const pixelmatch = ;const img1 = PNGsync;const img2 = PNGsync;const width height = img1;const diff = width height;;fs;
const img1 = img1Context;const img2 = img2Context;const diff = diffContext;;diffContext;
Install with NPM:
npm install pixelmatch
Use in the browser from a CDN: