如何比较网络上的图像以查看它们在 python3 中是否相同?
How can I compare images on the web to see if they are the same in python3?
我正在尝试编写一个脚本来比较图像,并告诉我图像是否相同。这是一些最小的代码:
import requests
url1 = 'https://scontent-lga3-1.cdninstagram.com/vp/b4577921aa35369af8980a3d563e4373/5DAE3C31/t51.2885-15/fr/e15/s1080x1080/66126877_342437073345261_1373504971257332049_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
url2 = 'https://scontent-lga3-1.cdninstagram.com/vp/fab3372181d5ad596280d2c095a3496e/5DE99775/t51.2885-15/e35/67547020_369706770411768_8601267197685673619_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
print(requests.Session().get(url1).content == requests.Session().get(url2).content)
但是,如果您手动导航到每个 url,您会发现照片是一样的。我的问题;我可以比较这些图像而不必将它们保存到目录吗?我在考虑也许将这些图像都读取为二进制文件,然后进行比较,但是我不知道如何即时。感谢所有提前回复的人。
如果你想看看两张图片是否完全一样,你可以使用BytesIO和PIL
import requests
from io import BytesIO
from PIL import Image
def get_image_data(img_url):
img = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
byteio = BytesIO()
img.save(byteio, format='PNG')
return byteio.getvalue()
url1 = 'https://scontent-lga3-1.cdninstagram.com/vp/b4577921aa35369af8980a3d563e4373/5DAE3C31/t51.2885-15/fr/e15/s1080x1080/66126877_342437073345261_1373504971257332049_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
url2 = 'https://scontent-lga3-1.cdninstagram.com/vp/fab3372181d5ad596280d2c095a3496e/5DE99775/t51.2885-15/e35/67547020_369706770411768_8601267197685673619_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
print(get_image_data(url1)==get_image_data(url2))
虽然这些图像之间似乎有细微差别,但此代码 returns 错误。
最好采用不依赖于图像大小的方法。您的两张图片可以是缩略图和全尺寸图片。
我会结合以下方法:
@坦纳克拉克 Image comparison - fast algorithm
和上面的方法:
import requests
from io import BytesIO
from PIL import Image, ImageFilter
import imagehash
def get_image(img_url):
img = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
byteio = BytesIO()
img.save(byteio, format='PNG')
return img
url1 = 'http://scontent-lga3-1.cdninstagram.com/vp/b4577921aa35369af8980a3d563e4373/5DAE3C31/t51.2885-15/fr/e15/s1080x1080/66126877_342437073345261_1373504971257332049_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
url2 = 'http://scontent-lga3-1.cdninstagram.com/vp/fab3372181d5ad596280d2c095a3496e/5DE99775/t51.2885-15/e35/67547020_369706770411768_8601267197685673619_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
def compare_images(url1, url2):
img1 = get_image(url1)
img2 = get_image(url2)
if img1.width<img2.width:
img2=img2.resize((img1.width,img1.height))
else:
img1=img1.resize((img2.width,img2.height))
img1=img1.filter(ImageFilter.BoxBlur(radius=3))
img2=img2.filter(ImageFilter.BoxBlur(radius=3))
phashvalue=imagehash.phash(img1)-imagehash.phash(img2)
ahashvalue=imagehash.average_hash(img1)-imagehash.average_hash(img2)
threshold = 1 # some experimentally valid value
totalaccuracy=phashvalue+ahashvalue
print(totalaccuracy)
return totalaccuracy <= threshold
print(compare_images(url1, url2))
我正在尝试编写一个脚本来比较图像,并告诉我图像是否相同。这是一些最小的代码:
import requests
url1 = 'https://scontent-lga3-1.cdninstagram.com/vp/b4577921aa35369af8980a3d563e4373/5DAE3C31/t51.2885-15/fr/e15/s1080x1080/66126877_342437073345261_1373504971257332049_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
url2 = 'https://scontent-lga3-1.cdninstagram.com/vp/fab3372181d5ad596280d2c095a3496e/5DE99775/t51.2885-15/e35/67547020_369706770411768_8601267197685673619_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
print(requests.Session().get(url1).content == requests.Session().get(url2).content)
但是,如果您手动导航到每个 url,您会发现照片是一样的。我的问题;我可以比较这些图像而不必将它们保存到目录吗?我在考虑也许将这些图像都读取为二进制文件,然后进行比较,但是我不知道如何即时。感谢所有提前回复的人。
如果你想看看两张图片是否完全一样,你可以使用BytesIO和PIL
import requests
from io import BytesIO
from PIL import Image
def get_image_data(img_url):
img = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
byteio = BytesIO()
img.save(byteio, format='PNG')
return byteio.getvalue()
url1 = 'https://scontent-lga3-1.cdninstagram.com/vp/b4577921aa35369af8980a3d563e4373/5DAE3C31/t51.2885-15/fr/e15/s1080x1080/66126877_342437073345261_1373504971257332049_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
url2 = 'https://scontent-lga3-1.cdninstagram.com/vp/fab3372181d5ad596280d2c095a3496e/5DE99775/t51.2885-15/e35/67547020_369706770411768_8601267197685673619_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
print(get_image_data(url1)==get_image_data(url2))
虽然这些图像之间似乎有细微差别,但此代码 returns 错误。
最好采用不依赖于图像大小的方法。您的两张图片可以是缩略图和全尺寸图片。
我会结合以下方法: @坦纳克拉克 Image comparison - fast algorithm 和上面的方法:
import requests
from io import BytesIO
from PIL import Image, ImageFilter
import imagehash
def get_image(img_url):
img = Image.open(requests.get(img_url, stream=True).raw).convert('RGB')
byteio = BytesIO()
img.save(byteio, format='PNG')
return img
url1 = 'http://scontent-lga3-1.cdninstagram.com/vp/b4577921aa35369af8980a3d563e4373/5DAE3C31/t51.2885-15/fr/e15/s1080x1080/66126877_342437073345261_1373504971257332049_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
url2 = 'http://scontent-lga3-1.cdninstagram.com/vp/fab3372181d5ad596280d2c095a3496e/5DE99775/t51.2885-15/e35/67547020_369706770411768_8601267197685673619_n.jpg?_nc_ht=scontent-lga3-1.cdninstagram.com'
def compare_images(url1, url2):
img1 = get_image(url1)
img2 = get_image(url2)
if img1.width<img2.width:
img2=img2.resize((img1.width,img1.height))
else:
img1=img1.resize((img2.width,img2.height))
img1=img1.filter(ImageFilter.BoxBlur(radius=3))
img2=img2.filter(ImageFilter.BoxBlur(radius=3))
phashvalue=imagehash.phash(img1)-imagehash.phash(img2)
ahashvalue=imagehash.average_hash(img1)-imagehash.average_hash(img2)
threshold = 1 # some experimentally valid value
totalaccuracy=phashvalue+ahashvalue
print(totalaccuracy)
return totalaccuracy <= threshold
print(compare_images(url1, url2))