Skip to content

Commit ebfbdf6

Browse files
committed
Merge branch 'coreman14/master'
Close #144
2 parents 9506246 + 69e3ac1 commit ebfbdf6

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

pixelmatch/contrib/PIL.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def pixelmatch(
1717
aa_color: RGBTuple = (255, 255, 0),
1818
diff_color: RGBTuple = (255, 0, 0),
1919
diff_mask: bool = False,
20+
fail_fast: bool = False,
2021
) -> int:
2122
"""
2223
Compares two images, writes the output diff and returns the number of mismatched pixels.
@@ -36,7 +37,8 @@ def pixelmatch(
3637
defaults to (255, 0, 0) (red)
3738
:param diff_mask: whether or not to draw the diff over a transparent background (a mask),
3839
defaults to False
39-
:return: number of pixels that are different
40+
:param fail_fast: if true, will return after first different pixel. Defaults to false
41+
:return: number of pixels that are different or 1 if fail_fast == true
4042
"""
4143
width, height = img1.size
4244
img1 = from_PIL_to_raw_data(img1)
@@ -59,6 +61,7 @@ def pixelmatch(
5961
aa_color=aa_color,
6062
diff_color=diff_color,
6163
diff_mask=diff_mask,
64+
fail_fast=fail_fast,
6265
)
6366

6467
if raw_output is not None and output is not None:

pixelmatch/core.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def pixelmatch(
1616
aa_color: RGBTuple = (255, 255, 0),
1717
diff_color: RGBTuple = (255, 0, 0),
1818
diff_mask: bool = False,
19+
fail_fast: bool = False,
1920
) -> int:
2021
"""
2122
Compares two images, writes the output diff and returns the number of mismatched pixels.
@@ -37,7 +38,8 @@ def pixelmatch(
3738
defaults to (255, 0, 0) (red)
3839
:param diff_mask: whether or not to draw the diff over a transparent background (a mask),
3940
defaults to False
40-
:return: number of pixels that are different
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
4143
"""
4244

4345
if len(img1) != len(img2):
@@ -93,6 +95,8 @@ def pixelmatch(
9395
# found substantial difference not caused by anti-aliasing; draw it as red
9496
if output:
9597
draw_pixel(output, pos, diffR, diffG, diffB)
98+
if fail_fast:
99+
return 1
96100
diff += 1
97101

98102
elif output:

test_pixelmatch.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,25 @@ def test_pixelmatch(
8383
assert mismatch == mismatch2, "number of mismatched pixels without diff"
8484

8585

86+
@pytest.mark.parametrize(
87+
"img_path_1,img_path_2,diff_path,options,expected_mismatch", testdata
88+
)
89+
def test_pixelmatch_failfast(
90+
img_path_1: str,
91+
img_path_2: str,
92+
diff_path: str,
93+
options: Dict,
94+
expected_mismatch: int,
95+
benchmark,
96+
):
97+
img1 = read_img(img_path_1)
98+
img2 = read_img(img_path_2)
99+
width, height = img1.size
100+
img1_data = pil_to_flatten_data(img1)
101+
img2_data = pil_to_flatten_data(img2)
102+
if expected_mismatch:
103+
assert benchmark(pixelmatch, img1_data, img2_data, width, height, fail_fast=True, **options) == 1
104+
86105
@pytest.mark.parametrize(
87106
"img_path_1,img_path_2,diff_path,options,expected_mismatch", testdata
88107
)

0 commit comments

Comments
 (0)