Skip to content

Add fail_fast option and added test #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pixelmatch/contrib/PIL.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def pixelmatch(
aa_color: RGBTuple = (255, 255, 0),
diff_color: RGBTuple = (255, 0, 0),
diff_mask: bool = False,
fail_fast: bool = False,
) -> int:
"""
Compares two images, writes the output diff and returns the number of mismatched pixels.
Expand All @@ -36,7 +37,8 @@ def pixelmatch(
defaults to (255, 0, 0) (red)
:param diff_mask: whether or not to draw the diff over a transparent background (a mask),
defaults to False
:return: number of pixels that are different
:param fail_fast: if true, will return after first different pixel. Defaults to false
:return: number of pixels that are different or 1 if fail_fast == true
"""
width, height = img1.size
img1 = from_PIL_to_raw_data(img1)
Expand All @@ -59,6 +61,7 @@ def pixelmatch(
aa_color=aa_color,
diff_color=diff_color,
diff_mask=diff_mask,
fail_fast=fail_fast,
)

if raw_output is not None and output is not None:
Expand Down
6 changes: 5 additions & 1 deletion pixelmatch/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def pixelmatch(
aa_color: RGBTuple = (255, 255, 0),
diff_color: RGBTuple = (255, 0, 0),
diff_mask: bool = False,
fail_fast: bool = False,
) -> int:
"""
Compares two images, writes the output diff and returns the number of mismatched pixels.
Expand All @@ -37,7 +38,8 @@ def pixelmatch(
defaults to (255, 0, 0) (red)
:param diff_mask: whether or not to draw the diff over a transparent background (a mask),
defaults to False
:return: number of pixels that are different
:param fail_fast: if true, will return after first different pixel. Defaults to false
:return: number of pixels that are different or 1 if fail_fast == true
"""

if len(img1) != len(img2):
Expand Down Expand Up @@ -93,6 +95,8 @@ def pixelmatch(
# found substantial difference not caused by anti-aliasing; draw it as red
if output:
draw_pixel(output, pos, diffR, diffG, diffB)
if fail_fast:
return 1
diff += 1

elif output:
Expand Down
1 change: 1 addition & 0 deletions test_pixelmatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def pil_to_flatten_data(img):
12437,
],
["3a", "3b", "3diff", OPTIONS, 212],
["3a", "3b", "3diff", {"fail_fast": True, "threshold": 0.05}, 1],
["4a", "4b", "4diff", OPTIONS, 36049],
["5a", "5b", "5diff", OPTIONS, 0],
["6a", "6b", "6diff", OPTIONS, 51],
Expand Down