@@ -72,6 +72,8 @@ def compare_l2_norm_masked(source, capture, mask) -> float:
72
72
# The L2 Error is summed across all pixels, so this normalizes
73
73
max_error = (3 * np .count_nonzero (mask ) * 255 * 255 ) ** 0.5
74
74
75
+ if not max_error :
76
+ return 0
75
77
return 1 - (error / max_error )
76
78
77
79
def compare_template (source , capture ) -> float :
@@ -155,10 +157,20 @@ def compare_phash_masked(source, capture, mask):
155
157
source_hash = imagehash .phash (source )
156
158
capture_hash = imagehash .phash (capture )
157
159
160
+ if not source_hash + capture_hash :
161
+ return 0
158
162
return 1 - ((source_hash - capture_hash ) / 64.0 )
159
163
160
164
161
165
def checkIfImageHasTransparency (image ):
162
- # TODO check for first transparent pixel, no need to iterate through the whole image
163
166
# 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