Skip to main content

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:

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, or None if don't need a diff image. Note that all three images need to have the same dimensions.
  • threshold — Matching threshold, ranges from 0 to 1. Smaller values make the comparison more sensitive. 0.1 by default.
  • includeAA — If true, disables detecting and ignoring anti-aliased pixels. false by default.
  • alpha — Blending factor of unchanged pixels in the diff output. Ranges from 0 for pure white to 1 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).
  • fail_fast - If true, will return after first different pixel.

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
https://github.com/whtsky/pixelmatch-py/raw/master/fixtures/4a.png https://github.com/whtsky/pixelmatch-py/raw/master/fixtures/4b.png 1diff
https://github.com/whtsky/pixelmatch-py/raw/master/fixtures/3a.png https://github.com/whtsky/pixelmatch-py/raw/master/fixtures/3b.png 1diff
https://github.com/whtsky/pixelmatch-py/raw/master/fixtures/6a.png https://github.com/whtsky/pixelmatch-py/raw/master/fixtures/6b.png 1diff
https://github.com/whtsky/pixelmatch-py/raw/master/fixtures/7a.png https://github.com/whtsky/pixelmatch-py/raw/master/fixtures/7b.png 1diff

Changelog

v0.3.1

  • fix: remove use of deprecated Pillow function #178 (@brianhelba)
  • chore: drop EOL Python versions (3.7-3.9), add support for 3.10-3.14 #171

v0.3.0

  • feat: add fail_fast option #144

v0.2.4

  • type: fix typing issues
  • chore: test Python 3.10

v0.2.3

  • feat: make package comply with PEP-561

v0.2.2

  • typing: use Sequence instead of List for RGBTuple
  • 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

pixelmatch-0.3.1.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pixelmatch-0.3.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file pixelmatch-0.3.1.tar.gz.

File metadata

  • Download URL: pixelmatch-0.3.1.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pixelmatch-0.3.1.tar.gz
Algorithm Hash digest
SHA256 cbae6f7fedcc6e77c36783c2638f1e956c26c0d0179b4186bb1b81098e4e66fc
MD5 eb773effd26ffea2c39d8d28cf6d3ec8
BLAKE2b-256 62767fc321f805117ab1de302aed3f5b97a502195351efee3e5fa97a1d3c417a

See more details on using hashes here.

Provenance

The following attestation bundles were made for pixelmatch-0.3.1.tar.gz:

Publisher: publish.yml on whtsky/pixelmatch-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file pixelmatch-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: pixelmatch-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 9.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for pixelmatch-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d53de8a09ff34ca3ffd86c1144a7015ccb096c8b6b8fb168edd2d9033da7f722
MD5 bade1e6d6f516016ef048b9fbef786a3
BLAKE2b-256 dfb5b7e17147c329e294f8675461630bd3db5d78d46c1256c4cbce82defc8476

See more details on using hashes here.

Provenance

The following attestation bundles were made for pixelmatch-0.3.1-py3-none-any.whl:

Publisher: publish.yml on whtsky/pixelmatch-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page