The smallest, simplest and fastest JavaScript pixel-level image comparison library
Installations
npm install pixelmatch
Developer
mapbox
Developer Guide
Module System
ESM
Min. Node Version
Typescript Support
No
Node Version
20.14.0
NPM Version
10.8.0
Statistics
6,201 Stars
121 Commits
306 Forks
163 Watching
1 Branches
546 Contributors
Updated on 27 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
534,703,789
Last day
-2.2%
502,433
Compared to previous day
Last week
4.5%
2,720,329
Compared to previous week
Last month
6.6%
11,285,635
Compared to previous month
Last year
4%
121,629,713
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
Dependencies
1
Dev Dependencies
2
pixelmatch
The smallest, simplest and fastest 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 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).
1const numDiffPixels = pixelmatch(img1, img2, diff, 800, 600, {threshold: 0.1});
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)
Demo
Example output
expected | actual | diff |
---|---|---|
API
pixelmatch(img1, img2, output, width, height[, options])
img1
,img2
— Image data of the images to compare (Buffer
,Uint8Array
orUint8ClampedArray
). Note: image dimensions must be equal.output
— Image data to write the diff to, ornull
if 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 from0
to1
. Smaller values make the comparison more sensitive.0.1
by default.includeAA
— Iftrue
, disables detecting and ignoring anti-aliased pixels.false
by default.alpha
— Blending factor of unchanged pixels in the diff output. Ranges from0
for pure white to1
for original brightness.0.1
by 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
.null
by 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).
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:
1pixelmatch image1.png image2.png output.png 0.1
Example usage
Node.js
1import fs from 'fs'; 2import {PNG} from 'pngjs'; 3import pixelmatch from 'pixelmatch'; 4 5const img1 = PNG.sync.read(fs.readFileSync('img1.png')); 6const img2 = PNG.sync.read(fs.readFileSync('img2.png')); 7const {width, height} = img1; 8const diff = new PNG({width, height}); 9 10pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1}); 11 12fs.writeFileSync('diff.png', PNG.sync.write(diff));
Browsers
1const img1 = img1Context.getImageData(0, 0, width, height); 2const img2 = img2Context.getImageData(0, 0, width, height); 3const diff = diffContext.createImageData(width, height); 4 5pixelmatch(img1.data, img2.data, diff.data, width, height, {threshold: 0.1}); 6 7diffContext.putImageData(diff, 0, 0);
Install
Install with NPM:
1npm install pixelmatch
Or use in the browser from a CDN:
1<script type="module"> 2 import pixelmatch from 'https://esm.run/pixelmatch';
Changelog
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: ISC License: LICENSE:0
Reason
security policy file detected
Details
- Info: security policy file detected: github.com/mapbox/.github/SECURITY.md:1
- Info: Found linked content: github.com/mapbox/.github/SECURITY.md:1
- Info: Found disclosure, vulnerability, and/or timelines in security policy: github.com/mapbox/.github/SECURITY.md:1
- Info: Found text in security policy: github.com/mapbox/.github/SECURITY.md:1
Reason
1 existing vulnerabilities detected
Details
- Warn: Project is vulnerable to: GHSA-3xgq-45jj-v275
Reason
branch protection is not maximal on development and all release branches
Details
- Info: 'allow deletion' disabled on branch 'main'
- Info: 'force pushes' disabled on branch 'main'
- Warn: 'branch protection settings apply to administrators' is disabled on branch 'main'
- Warn: 'stale review dismissal' is disabled on branch 'main'
- Warn: required approving review count is 1 on branch 'main'
- Warn: codeowners review is not required on branch 'main'
- Warn: 'last push approval' is disabled on branch 'main'
- Warn: no status checks found to merge onto branch 'main'
- Info: PRs are required in order to make changes on branch 'main'
Reason
Found 5/30 approved changesets -- score normalized to 1
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Warn: no topLevel permission defined: .github/workflows/node.yml:1
- Info: no jobLevel write permissions found
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:8: update your workflow using https://app.stepsecurity.io/secureworkflow/mapbox/pixelmatch/node.yml/main?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/node.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/mapbox/pixelmatch/node.yml/main?enable=pin
- Warn: npmCommand not pinned by hash: .github/workflows/node.yml:17
- Info: 0 out of 2 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 1 npmCommand dependencies pinned
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 5 are checked with a SAST tool
Score
4.5
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn MoreOther packages similar to pixelmatch
@types/pixelmatch
TypeScript definitions for pixelmatch
@loki/diff-pixelmatch
Loki pixelmatch image differ
codeceptjs-pixelmatchhelper
Pixelmatch helper for CodeceptJS, with support for Playwright, Webdriver, TestCafe, Puppeteer & Appium
dynamicpixelmatch
Exclude some areas from image before comparison using pixelmatch