From 18c6899aabecd6cc0400c85e88780e651a8b6a85 Mon Sep 17 00:00:00 2001 From: Dou Du Date: Tue, 17 Dec 2024 15:58:33 +0200 Subject: [PATCH] update the image test with image similarity comparson --- .github/workflows/screenshot-comparison.yml | 1 + test/test_figures.py | 31 +++++++++++++++------ 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/.github/workflows/screenshot-comparison.yml b/.github/workflows/screenshot-comparison.yml index 9bf19a0..1ebceec 100644 --- a/.github/workflows/screenshot-comparison.yml +++ b/.github/workflows/screenshot-comparison.yml @@ -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: | diff --git a/test/test_figures.py b/test/test_figures.py index b1e208d..c2d35c5 100644 --- a/test/test_figures.py +++ b/test/test_figures.py @@ -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.") +