A pixel-level image comparison library.
Project description
pixelmatch-py
A fast pixel-level image comparison library, originally created to compare screenshots in tests. Now with additional support of PIL.Image instances Python port of https://github.com/mapbox/pixelmatch.
Features accurate anti-aliased pixels detection and perceptual color difference metrics.
from pixelmatch import pixelmatch
num_diff_pixels = pixelmatch(img1, img2, 800, 600, diff, 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)
Install
python -m pip install pixelmatch
Example usage
PIL.Image comparison
from PIL import Image
from pixelmatch.contrib.PIL import pixelmatch
img_a = Image.open("a.png")
img_b = Image.open("b.png")
img_diff = Image.new("RGBA", img_a.size)
# note how there is no need to specify dimensions
mismatch = pixelmatch(img_a, img_b, img_diff, includeAA=True)
img_diff.save("diff.png")
Raw Image Data Comparison
from pixelmatch import pixelmatch
width, height = 1920, 1080
img_a = [R1, G1, B1, A1, R2, B2, G2, A2, ...]
img_b = [R1, G1, B1, A1, R2, B2, G2, A2, ...]
data_diff = [0] * len(img_a)
mismatch = pixelmatch(img_a, img_b, width, height, data_diff, includeAA=True)
API
pixelmatch(img1, img2, width, height, output, threshold, includeAA, alpha, aa_color, diff_color, diff_mask)
img1
,img2
— RGBA Image data of the images to compare. Note: image dimensions must be equal.width
,height
— Width and height of the images.output
— Image data to write the diff to, orNone
if don't need a diff image. Note that all three images need to have the same dimensions.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.aa_color
— The color of anti-aliased pixels in the diff output in[R, G, B]
format.[255, 255, 0]
by default.diff_color
— The color of differing pixels in the diff output in[R, G, B]
format.[255, 0, 0]
by default.diff_mask
— 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.
contrib.PIL.pixelmatch
Compares two images, writes the output diff and returns the number of mismatched pixels. Exact same API as pixelmatch.pixelmatch
except for the important fact that it takes instances of PIL.Image for image parameters (img1
, img2
, and output
) and the width/size need not be specified.
Example output
expected | actual | diff |
---|---|---|
Changelog
v0.2.2
- typing: use
Sequence
instead ofList
forRGBTuple
- build: switch to
poetry_core
#81
v0.2.1
- feat: add function to compare PIL.Image instances through contrib.PIL.pixelmatch #42
v0.2.0
- BREAKING CHANGE: remove
options
parameter #38 - docs: use absolute url for images in README
v0.1.1
- fix: fix bug in fast path #18
v0.1.0
- Initial release
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file pixelmatch-0.2.2.tar.gz
.
File metadata
- Download URL: pixelmatch-0.2.2.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b60019b42b9924cd6cd374006f381f75b701092a6b85fde3ce74acee58d8c38e |
|
MD5 | 9b7b016ba0747e5d3d97542c931857de |
|
BLAKE2b-256 | ae01c6dd97e82c3704751ffde78f43350787b947459abe938af5907dfdd17267 |
File details
Details for the file pixelmatch-0.2.2-py3-none-any.whl
.
File metadata
- Download URL: pixelmatch-0.2.2-py3-none-any.whl
- Upload date:
- Size: 8.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.6.1 requests/2.25.1 setuptools/51.1.0.post20201221 requests-toolbelt/0.9.1 tqdm/4.54.1 CPython/3.9.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 43fc71239b5cba13598321f1975c922fa67ef40f6fff6ac3866baedc80a9633c |
|
MD5 | ee21b31e7642a0da842c70abb6b9c047 |
|
BLAKE2b-256 | 4d62a0f203a823eddd8bc73846671eef17e82d2128d5500a07cb4f7685e7c8c2 |