Skip to content

Commit f1cb1a6

Browse files
committed
Fix #52 Comparisons with empty masks
1 parent 43d5535 commit f1cb1a6

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/compare.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ def compare_l2_norm_masked(source, capture, mask) -> float:
7272
# The L2 Error is summed across all pixels, so this normalizes
7373
max_error = (3 * np.count_nonzero(mask) * 255 * 255) ** 0.5
7474

75+
if not max_error:
76+
return 0
7577
return 1 - (error / max_error)
7678

7779
def compare_template(source, capture) -> float:
@@ -155,10 +157,20 @@ def compare_phash_masked(source, capture, mask):
155157
source_hash = imagehash.phash(source)
156158
capture_hash = imagehash.phash(capture)
157159

160+
if not source_hash + capture_hash:
161+
return 0
158162
return 1 - ((source_hash - capture_hash) / 64.0)
159163

160164

161165
def checkIfImageHasTransparency(image):
162-
# TODO check for first transparent pixel, no need to iterate through the whole image
163166
# Check if there's a transparency channel (4th channel) and if at least one pixel is transparent (< 255)
164-
return image.shape[2] == 4 and np.mean(image[:, :, 3]) != 255
167+
if image.shape[2] != 4:
168+
return False
169+
mean = np.mean(image[:, :, 3])
170+
if mean == 0:
171+
# Non-transparent images code path is usually faster and simpler, so let's return that
172+
return False
173+
# TODO error message if all pixels are transparent
174+
# (the image appears as all black in windows, so it's not obvious for the user what they did wrong)
175+
176+
return mean != 255

0 commit comments

Comments
 (0)