Compare images Python PIL

老子叫甜甜 提交于 2020-12-29 09:12:51

问题


How can I compare two images? I found Python's PIL library, but I do not really understand how it works.


回答1:


To check does jpg files are exactly the same use pillow library:

from PIL import Image
from PIL import ImageChops

image_one = Image.open(path_one)
image_two = Image.open(path_two)

diff = ImageChops.difference(image_one, image_two)

if diff.getbbox():
    print("images are different")
else:
    print("images are the same")



回答2:


based on Victor Ilyenko's answer, I needed to convert the images to RGB as PIL fails silently when the .png you're trying to compare contains a alpha channel.

image_one = Image.open(path_one).convert('RGB')
image_two = Image.open(path_two).convert('RGB')


来源:https://stackoverflow.com/questions/35176639/compare-images-python-pil

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!