Debian Codegraph @

pixelmatch/core.py

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