Skip to content

Commit

Permalink
update the image test with image similarity comparson
Browse files Browse the repository at this point in the history
  • Loading branch information
Dou Du committed Dec 17, 2024
1 parent 9ef7022 commit 18c6899
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
1 change: 1 addition & 0 deletions .github/workflows/screenshot-comparison.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ jobs:
pip install --upgrade pytest
pip install --upgrade selenium
pip install --upgrade Pillow
pip install --upgrade imagededup
- uses: nanasess/setup-chromedriver@master
- run: |
Expand Down
31 changes: 23 additions & 8 deletions test/test_figures.py
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.")

0 comments on commit 18c6899

Please sign in to comment.