A pixel-level image comparison library. Python port of https://github.com/mapbox/pixelmatch
Project description
pixelmatch-py
Python port of https://github.com/mapbox/pixelmatch.
A fast pixel-level image comparison library, originally created to compare screenshots in tests.
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
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, orNoneif don't need a diff image. Note that all three images need to have the same dimensions.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.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.
Example usage
PIL
from PIL import Image
from pixelmatch import pixelmatch
def pil_to_flatten_data(img):
"""
Convert data from [(R1, G1, B1, A1), (R2, G2, B2, A2)] to [R1, G1, B1, A1, R2, G2, B2, A2]
"""
return [x for p in img.convert("RGBA").getdata() for x in p]
img_a = Image.open("a.png")
img_b = Image.open("b.png")
width, height = img_a.size
data_a = pil_to_flatten_data(img_a)
data_b = pil_to_flatten_data(img_b)
data_diff = [0] * len(data_a)
mismatch = pixelmatch(data_a, data_b, width, height, data_diff, includeAA=True)
img_diff = Image.new("RGBA", img_a.size)
def flatten_data_to_pil(data):
return list(zip(data[::4], data[1::4], data[2::4], data[3::4]))
img_diff.putdata(flatten_data_to_pil(data_diff))
img_diff.save("diff.png")
Example output
| expected | actual | diff |
|---|---|---|
Changelog
v0.2.0
-
BREAKING CHANGE: remove
optionsparameter #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
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pixelmatch-0.2.0.tar.gz.
File metadata
- Download URL: pixelmatch-0.2.0.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
357dcfcb2936d97b98dc3285ae98ebb4d8607b3a52af67f27fa652fd7a66b8dd
|
|
| MD5 |
780bb39557af985135502280383eae73
|
|
| BLAKE2b-256 |
b3c3d1c3bcef8fa10a8117a7ee2173287580e229daf4d6bfd6aa0e35bb133402
|
File details
Details for the file pixelmatch-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pixelmatch-0.2.0-py3-none-any.whl
- Upload date:
- Size: 6.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/45.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a33118eafae1142d1aab4362cb762b9934059c0f4beff856decb0d1f066acc9d
|
|
| MD5 |
62405258f04db23f758d08bef9fdc008
|
|
| BLAKE2b-256 |
98a8c2fb13cebda2d3d134912931ad60d3d16eb04c31991dc1023b344d9235b3
|