Skip to content

Fix #52 Comparisons with empty masks #72

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 2 commits into from
Nov 28, 2021
Merged
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
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