I have a setting where I want to compare two images' URLs found in a website and I want to find out if they contain the same image, regardless of the image size.
If the two images have the same size, I simply expect the two arrays to be identical.
However, my problem is how to detect if they are the same image when the size is different? (Example: URL1, URL2).
from PIL import Image
import requests
import numpy as np
def get_img_content(img_url):
try:
im = np.asarray(Image.open(requests.get(img_url, stream=True,timeout=(60,60)).raw))
return im
except:
return 0
def compare_images(img1,img2):
if isinstance(img1,(np.ndarray)) and isinstance(img2,(np.ndarray)):
if img1.shape == img2.shape:
comp = img1 == img2
return comp.all()
else:
#missing
else:
return False
img_url = 'https://i2.cdn.turner.com/money/dam/assets/140225160936-levin-mccain-1024x576.png'
img_url2 = 'https://i2.cdn.turner.com/money/dam/assets/140225160936-levin-mccain-640x360.png'
img1 = get_img_content(img_url)
img2 = get_img_content(img_url2)
print(compare_images(img1,img2))