-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update the image test with image similarity comparson
- Loading branch information
Dou Du
committed
Dec 17, 2024
1 parent
9ef7022
commit 18c6899
Showing
2 changed files
with
24 additions
and
8 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 |
---|---|---|
@@ -1,12 +1,27 @@ | ||
from PIL import Image, ImageChops, ImageStat | ||
from imagededup.methods import PHash | ||
|
||
image1 = Image.open('widget-01.png') | ||
image2 = Image.open('test/widget-sample.png') | ||
# Initialize the hashing method | ||
phasher = PHash() | ||
|
||
diff = ImageChops.difference(image1, image2) | ||
stat = ImageStat.Stat(diff) | ||
# Provide the paths to your images | ||
image1_path = 'widget-01.png' | ||
image2_path = 'test/widget-sample.png' | ||
|
||
if sum(stat.mean) == 0: | ||
print('images are the same') | ||
# Generate hashes for both images | ||
hash1 = phasher.encode_image(image_file=image1_path) | ||
hash2 = phasher.encode_image(image_file=image2_path) | ||
|
||
# Calculate the Hamming distance between the two hashes | ||
distance = phasher.hamming_distance(hash1, hash2) | ||
|
||
# Print similarity score | ||
print(f"Hamming distance between the two images: {distance}") | ||
|
||
# Interpret the similarity | ||
if distance == 0: | ||
print("The images are identical!") | ||
elif distance < 10: # Adjust threshold as needed | ||
print("The images are very similar.") | ||
else: | ||
raise Exception("The result is NOT the same as expected. Please check matplotlib version.") | ||
raise Exception("The result is NOT the same as expected. Please check matplotlib version.") | ||
|