Skip to content
This repository has been archived by the owner on Apr 26, 2023. It is now read-only.

Commit

Permalink
Fix #52 Comparisons with empty masks
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Nov 27, 2021
1 parent 43d5535 commit cc35c1b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/compare.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ def compare_l2_norm_masked(source, capture, mask) -> float:
# The L2 Error is summed across all pixels, so this normalizes
max_error = (3 * np.count_nonzero(mask) * 255 * 255) ** 0.5

if not max_error:
return 0
return 1 - (error / max_error)

def compare_template(source, capture) -> float:
Expand Down Expand Up @@ -155,10 +157,20 @@ def compare_phash_masked(source, capture, mask):
source_hash = imagehash.phash(source)
capture_hash = imagehash.phash(capture)

if not source_hash + capture_hash:
return 0
return 1 - ((source_hash - capture_hash) / 64.0)


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

return mean != 255

0 comments on commit cc35c1b

Please sign in to comment.