A pixel-level image comparison library.
Maintained. Last release 9 months ago, commits in 12 of the last 26 weeks. Weaker on release cadence.
pip install pixelmatchpixelmatch-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, fail_fast)
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 from to . Smaller values make the comparison more sensitive. by default.