Python Forum
Compare two images with Python is possible? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Compare two images with Python is possible? (/thread-23227.html)



Compare two images with Python is possible? - Delemir78 - Dec-17-2019

Hi everyone, I would like to write a code to compare two photographs (same dimensions) in order to understand if they are similar or not.

I take photographs at intervals of several minutes and I would like to write a code that verifies that the last photo is similar to the previous one and that otherwise, if for example an obstacle is placed in front of the frame, it sends an alarm message.

from PIL import Image
from PIL import ImageChops

img1 = Image.open("file1")
img2 = Image.open("file2")

diff = ImageChops.difference(im2, im1)

diff.save("file3")
But this code shows me a photo with different pixels and I don't need it.

Does anyone have any advice?


RE: Compare two images with Python is possible? - Larz60+ - Dec-17-2019

Quote:PIL.ImageChops.difference(image1, image2)

Returns the absolute value of the pixel-by-pixel difference between the two images.

out = abs(image1 - image2)

Return type: Image
key words here: Returns and Return type: Image
sounds to me that this is exactly what it is supposed to do, along with the abs modification


RE: Compare two images with Python is possible? - Delemir78 - Dec-17-2019

Thanks for the reply, so should I write my code like this?

from PIL import Image
from PIL import ImageChops
 
img1 = Image.open("file1")
img2 = Image.open("file2")
 
out = abs(im1 - img2)



RE: Compare two images with Python is possible? - Larz60+ - Dec-17-2019

In your previous call diff = ImageChops.difference(im2, im1),
diff is the abs value! it's an image, but the image is the absolute pixel-by-pixel difference!