简体   繁体   中英

how to compare an image with a folder of images ( then do a certain thing when they are 50% alike or something )

so i was wondering if there was sth like

pyautogui.locateOnScreen('picuture.jpg',confidence=x)

I'm currently trying to compare pictures from a folder, but pyautogui only works with "onScreen" images. I don't want to check if the picture are 1:1 the same, but if they are alike, with pyautogui you can simply add the "confidence" parameter and i've built my script based on that i just wanted to know if someone knows a way to do that.

You can use numpy to compare the pixel array of two images.

from PIL import Image
import numpy as np

# import the image as pixels
img_a = Image.open('a.jpg')
img_b = Image.open('b.jpg')
img_a_pixels = img_a.load()
img_b_pixels = img_b.load()

# transform them into numpy array
img_a_array = np.array(img_a_pixels)
img_b_array = np.array(img_b_pixels)

# compare the difference
difference = (img_a_array == img_b_array).sum()

Then you can see if the difference exceeds your threshold. If it does not, you can consider them similar.

This code checks if there are any duplicates in a folder its a bit slow though.

    import image_similarity_measures
    from image_similarity_measures.quality_metrics import rmse, psnr
    from sewar.full_ref import rmse, psnr
    import cv2
    import os 
    import time



    def check(path_orginal,path_new):#give r strings 
        original = cv2.imread(path_orginal)
        new = cv2.imread(path_new)
        return  rmse(original, new)

    def folder_check(folder_path):
        i=0
        file_list = os.listdir(folder_path)
        print(file_list)
        duplicate_dict={}
        for file in file_list:
            # print(file)
            file_path=os.path.join(folder_path,file)
            for file_compare in file_list:
                print(i)
                i+=1
                file_compare_path=os.path.join(folder_path,file_compare)
                if file_compare!=file:
                    similarity_score=check(file_path,file_compare_path)
                    # print(str(similarity_score))
            
                    if similarity_score==0.0:
                         print(file,file_compare)
                         duplicate_dict[file]=file_compare
            file_list.remove(str(file))
        return duplicate_dict
     start_time=time.time()
     print(folder_check(r"C:\Users\Admin\Linear-Regression-1\image-similarity-measures\input1"))
     end_time=time.time()
     stamp=end_time-start_time
     print(stamp)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM