Python Forum

Full Version: Compare two images with Python is possible?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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
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)
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!