Migrate from pixelmatch-py
Python visual regression tests often use
pixelmatch-py (pixelmatch on
PyPI) to compare screenshots, and Pillow or pypng to read and write the PNG
files. pixelmatch-py is written in pure Python, so a comparison of two
1920 × 1080 screenshots takes more than a second. It also holds the GIL, so
other threads cannot compare images at the same time. anyformat-image
replaces it:
- The diff count and the diff image are the same as the results of
pixelmatch-py 0.4.0 when you set
algorithm="pixelmatch6". See Select the algorithm. - One call can decode two files, compare them, and encode the diff image. In the measurement below, this took about 30 ms. Pillow and pixelmatch-py took about 1.7 s.
compare_imagesreleases the GIL and uses all CPU cores. Thread pools andasyncio.to_threadcan run several comparisons in parallel.- The package has no runtime dependencies. You do not need Pillow or NumPy to read and write PNG files.
See the image API reference for the full API. To migrate from pngjs and pixelmatch in Node.js, read Migrate from pngjs and pixelmatch.
Install the package:
Compare two PNG files
With Pillow and pixelmatch-py:
With anyformat-image:
compare_images decodes the files itself. When the program also needs the
pixels of the two files, for example to draw a composite of the two images
and the diff, add decoded=True. The result then has the decoded pixels in
decoded_a and decoded_b, so the program does not decode the files again.
See Decoded inputs.
Compare images in memory
To compare images that are already in memory, wrap the pixels in a
RawImage. The pixels must be 8-bit RGBA, row by row, with width * height * 4
bytes. The raw API of pixelmatch-py uses the same layout, so you can give
the same bytes:
RawImage uses bytes without a copy and copies other buffers once, for
example a bytearray or a NumPy uint8 array with the shape
(height, width, 4). It does not accept a list of integers. Convert a list
with bytes(img1).
To compare Pillow images, convert them to RGBA first. To make a Pillow image
from the diff image, use Image.frombytes:
Select the algorithm
pixelmatch-py 0.4.0 is a port of pixelmatch 5 for JavaScript. It uses the
YIQ color metric and blends semi-transparent pixels with white.
algorithm="pixelmatch6" gives the same results as pixelmatch 6.0.0 and
5.3.0, which use the same metric, so your thresholds and baselines stay
valid.
The default algorithm, "pixelmatch8", uses the OKLab HyAB metric of
pixelmatch 8. Its counts are different for the same threshold. Use it only
when you can tune the thresholds again and make new baselines. See
Algorithms.
API mapping
The options have the same defaults as in pixelmatch-py. compare_images
checks them more strictly: threshold and alpha must be numbers from 0 to
1, and a color must be a sequence of three integers from 0 to 255. See
Compare options.
compare_images also has features that pixelmatch-py does not have:
diff_bounds: the smallest rectangle that contains all different pixels.antialiased_pixels: the number of different pixels that the comparison ignored as anti-aliasing.window_size: the largest number of different pixels in any N × N window, inwindow_diff_pixels.ignore_mask: pixels that the comparison skips, for example a clock or an ad in a screenshot. See Ignore mask.diff_color_alt: a second color for pixels that are darker in the second image.
Differences from pixelmatch-py
- Identical images with
contrib.PIL. When the two images are identical,contrib.PIL.pixelmatch0.4.0 draws the gray diff image with Pillow. Its bytes can differ by 1 or 2 from the bytes of the pixelmatch algorithm.compare_imagesalways uses the pixelmatch algorithm, as the rawpixelmatch.pixelmatchfunction does. The diff count is 0 in both cases. diff_maskwith an output that is not empty. pixelmatch-py writes only the different pixels whendiff_maskis set, and keeps the other bytes of the output.diff_imageis transparent at these pixels, so a program that fills the output first, for example with a copy of the first image, must draw the diff image over that copy itself.- Return value.
compare_imagesreturns aCompareResult, not a number, and it makes the diff image. It does not write into an output of the caller.
Decoding
compare_images, decode, and decode_file decode a PNG file in other
ways than Pillow in these cases:
- EXIF orientation.
decodeapplies the orientation of aneXIfchunk by default, as browsers do, so the width and the height can change. Pillow ignores it inImage.open. To get the pixels of Pillow, passorientation="ignore"todecode, ordecode=DecodeOptions(orientation="ignore")tocompare_images. - 16-bit samples and gray files with a
tRNSchunk. Pillow converts them in other ways. See Raw images.
When you compare two PNG files that were made by the same program, these
cases usually do not apply. Only PNG files can be decoded today. To compare
files of other formats, decode them with Pillow and give the pixels as a
RawImage.
Performance
A measurement on one x86_64 Linux computer (Intel Core i7-13700F, 24 threads) with Python 3.12, pixelmatch-py 0.4.0, Pillow 13.0.0, and pypng 0.20220715.0. The inputs were one synthetic 1920 × 1080 screenshot with text and rectangles, and a changed copy with 1933 different pixels. Each value is the median of several calls. A range shows the medians of three runs when they were not close.
Shorter is faster. Each operation has its own scale.
Show the data as a table
| Operation | Package | Time |
|---|---|---|
| Decode PNG to RGBA | pypng | 750 ms |
| Decode PNG to RGBA | Pillow | 18 ms |
| Decode PNG to RGBA | anyformat-image | 6 ms |
| Encode PNG | pypng | 31 ms |
| Encode PNG | Pillow | 56 ms |
| Encode PNG | anyformat-image | 17 ms |
| Compare raw images with a diff image | pixelmatch-py | 1610 ms |
| Compare raw images with a diff image | anyformat-image | 1.0–2.3 ms |
| Compare raw images with a diff image | anyformat-image, one thread | 3.2 ms |
| Decode two files, compare, encode the diff | pixelmatch-py (Pillow, contrib.PIL) | 1690 ms |
| Decode two files, compare, encode the diff | anyformat-image | 25–35 ms |
| Decode two files, compare, encode the diff | anyformat-image, one thread | 37 ms |
The diff counts and the diff images were identical. "One thread" is the
time with RAYON_NUM_THREADS=1. The encoded files had different sizes:
320 KiB with pypng, 408 KiB with Pillow, and 387 KiB with anyformat-image
(compression="balanced"). Measure with your own images if the file size
matters. See PNG options.