Debian Codegraph @

test/common/pixelmatch/core.py

raw
1 # Copyright (c) 2019, Mapbox, Wu Haotian
2 # Copyright (c) 2026, Freya Gustavsson
3 # SPDX-License-Identifier: ISC
4
5 from collections.abc import Collection
6 from typing import Optional
7
8 from .types import ImageArea, ImageSequence, MutableImageSequence, RGBTuple
9 from .utils import antialiased, color_delta, draw_gray_pixel, draw_pixel, ignorable_coord
10
11
12 def pixelmatch(
13 img1: ImageSequence,
14 img2: ImageSequence,
15 width: int,
16 height: int,
17 output: Optional[MutableImageSequence] = None,
18 threshold: float = 0.1,
19 includeAA: bool = False,
20 alpha: float = 0.1,
21 aa_color: RGBTuple = (255, 255, 0),
22 diff_color: RGBTuple = (255, 0, 0),
23 diff_mask: bool = False,
24 fail_fast: bool = False,
25 masked_areas: Collection[ImageArea] = [],
26 masked_areas_color: RGBTuple = (0, 255, 0)
27 ) -> int:
28 """
29 Compares two images, writes the output diff and returns the number of mismatched pixels.
30 'Raw image data' refers to a 1D, indexable collection of image data in the
31 format [R1, G1, B1, A1, R2, G2, ...].
32
33 :param img1: Image data to compare with img2. Must be the same size as img2
34 :param img2: Image data to compare with img2. Must be the same size as img1
35 :param width: Width of both images (they should be the same).
36 :param height: Height of both images (they should be the same).
37 :param output: Image data to write the diff to. Should be the same size as
38 :param threshold: matching threshold (0 to 1); smaller is more sensitive, defaults to 1
39 :param includeAA: whether or not to skip anti-aliasing detection, ie if includeAA is True,
40 detecting and ignoring anti-aliased pixels is disabled. Defaults to False
41 :param alpha: opacity of original image in diff output, defaults to 0.1
42 :param aa_color: tuple of RGB color of anti-aliased pixels in diff output,
43 defaults to (255, 255, 0) (yellow)
44 :param diff_color: tuple of RGB color of the color of different pixels in diff output,
45 defaults to (255, 0, 0) (red)
46 :param diff_mask: whether or not to draw the diff over a transparent background (a mask),
47 defaults to False
48 :param fail_fast: if true, will return after first different pixel. Defaults to false
49 :param masked_areas: collection of areas to ignore in (x, y, x_delta, y_delta)
50 :param masked_areas_color: tuple of RGB color of masked out pixels in diff output,
51 defaults to (0, 255, 0) (green)
52 :return: number of pixels that are different or 1 if fail_fast == true
53 """
54
55 if len(img1) != len(img2):
56 raise ValueError("Image sizes do not match.", len(img1), len(img2))
57 if output and len(output) != len(img1):
58 raise ValueError("Diff image size does not match img1 & img2.", len(img1), len(output))
59
60 if len(img1) != width * height * 4:
61 raise ValueError(
62 "Image data size does not match width/height.",
63 len(img1),
64 width * height * 4,
65 )
66
67 # fast path if identical
68 if img1 == img2:
69 if output and not diff_mask:
70 for i in range(width * height):
71 draw_gray_pixel(img1, 4 * i, alpha, output)
72
73 return 0
74
75 # maximum acceptable square distance between two colors;
76 # 35215 is the maximum possible value for the YIQ difference metric
77 maxDelta = 35215 * threshold * threshold
78
79 diff = 0
80 aaR, aaG, aaB = aa_color
81 diffR, diffG, diffB = diff_color
82 maskR, maskG, maskB = masked_areas_color
83
84 # compare each pixel of one image against the other one
85 for y in range(height):
86 for x in range(width):
87 pos = (y * width + x) * 4
88
89 # squared YUV distance between colors at this pixel position
90 delta = color_delta(img1, img2, pos, pos)
91
92 # the color difference is above the threshold
93 if delta > maxDelta:
94 # check it's a real rendering difference or just anti-aliasing
95 if not includeAA and (
96 antialiased(img1, x, y, width, height, img2) or antialiased(img2, x, y, width, height, img1)
97 ):
98 # one of the pixels is anti-aliasing; draw as yellow and do not count as difference
99 # note that we do not include such pixels in a mask
100 if output and not diff_mask:
101 draw_pixel(output, pos, aaR, aaG, aaB)
102 else:
103
104 # ignore masked coords
105 if (
106 ignorable_coord(x, y, masked_areas)
107 ):
108 if output:
109 draw_pixel(output, pos, maskR, maskG, maskB)
110 else:
111 # found substantial difference not caused by anti-aliasing; draw it as red
112 if output:
113 draw_pixel(output, pos, diffR, diffG, diffB)
114 if fail_fast:
115 return 1
116 diff += 1
117
118 elif output:
119 # pixels are similar; draw background as grayscale image blended with white
120 if not diff_mask:
121 draw_gray_pixel(img1, pos, alpha, output)
122
123 # return the number of different pixels
124 return diff