Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare images Python PIL

Tags:

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

like image 973
jackDanielle Avatar asked Feb 03 '16 12:02

jackDanielle


2 Answers

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") 
like image 141
Viktor Ilyenko Avatar answered Sep 18 '22 16:09

Viktor Ilyenko


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') 
like image 44
tha_tux Avatar answered Sep 22 '22 16:09

tha_tux