Python Forum
Compare two images with Python is possible?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare two images with Python is possible?
#1
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?
Reply
#2
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
Reply
#3
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)
Reply
#4
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare two Excel sheets with Python and list diffenrences dmkfon 1 14,488 Oct-09-2021, 03:30 PM
Last Post: Larz60+
  how to read multispectral images on python noorasim 0 1,754 Feb-28-2021, 03:54 PM
Last Post: noorasim
  How do I insert images in Python using gspread (or any other package)? ivansing23 0 2,225 Jul-27-2020, 01:26 PM
Last Post: ivansing23
  My journey with attaching file instead of images using Python numbnumb54 1 1,743 Jul-24-2020, 02:37 AM
Last Post: scidam
  A more efficient way of comparing two images in a python vukan 0 1,967 Mar-17-2020, 11:39 AM
Last Post: vukan
  how to compare two different size images in python and find corresponding pixel value squidsirymchenry 1 4,513 Feb-03-2020, 06:48 AM
Last Post: michael1789
  How to compare in python an input value with an hashed value in mysql table? Formationgrowthhacking 4 3,265 Jan-14-2020, 11:43 AM
Last Post: Formationgrowthhacking
  How to get first 5 images form the document using Python BeautifulSoup sarath_unrelax 0 1,615 Dec-19-2019, 07:13 AM
Last Post: sarath_unrelax
  How can I compare Python XML-Files and add missing values from one to another kirat 2 2,628 Aug-30-2019, 12:17 PM
Last Post: perfringo
  Python csv compare two file, update value if two value is matched kinojom 1 2,513 Apr-17-2019, 10:36 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020