This repository has been archived by the owner on Mar 21, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f798902
commit 82cf364
Showing
5 changed files
with
132 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from PIL import Image | ||
from typing import Union | ||
from io import BytesIO | ||
|
||
def calculate_hash(image_path: Union[str, bytes, BytesIO]) -> str: | ||
# Open image using PIL | ||
image = Image.open(image_path) | ||
|
||
# Resize image to a fixed size (e.g., 8x8 pixels) | ||
image = image.resize((8, 8), Image.ANTIALIAS) | ||
|
||
# Convert image to grayscale | ||
image = image.convert('L') | ||
|
||
# Calculate average pixel value | ||
average_pixel = sum(image.getdata()) / 64 | ||
|
||
# Generate binary hash | ||
hash_value = ''.join(['1' if pixel >= average_pixel else '0' for pixel in image.getdata()]) | ||
|
||
return hash_value | ||
|
||
def hamming_distance(hash1: str, hash2: str) -> float: | ||
# Calculate the Hamming distance between two hashes | ||
return sum(c1 != c2 for c1, c2 in zip(hash1, hash2)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters