简体   繁体   中英

how do i see the difference between two images in python using pil?

I'm trying to make a program that looks from my macbook's camera (or any camera) and lets me know when something happened (lights turned on/off, any motion, etc), the program takes a screenshot every 1 second and compares the last image taken with the one that it just took.

Here is the code in my while loop

while True:
    time.sleep(1)

    image = capture_image()

    if last_image == None:
        last_image = image

    # compare the two images

    print('image', image)
    print('last image', last_image)
    print('')

    last_image = image

Here is an example to know the difference between 2 pictures.Maybe it could solve your problem.

from PIL import Image
from PIL import ImageChops
img1 = Image.open(yourPath)
img2 = Image.open(yourPath)
# make sure img1,img2 have the same picture width and height.
diff = ImageChops.difference(img1, img2)
diff.show()

In your circumstance,this may works.

from PIL import ImageChops
While True:
    time.sleep(1)

.......

    diff = ImageChops.difference(Now_Image,Last_Image)
    If diff.getbbox() is None:
        print("Now_Image and Last_Image are same.")    
    # diff.show()
    # or you can handle the diff picture.

The diff Image will show the difference between those picture. The difference between two pictures will show you and the same component will be black.

This will give you the difference of two images but you certainly want to add some tolerance, maybe through a statistical measure. I am not sure you want to work with live images though, maybe separate your concerns by capturing images and then running a script a second later independently. Depends on your application purpose though.

from PIL import Image
import numpy as np

image1Url = "./img1.jpg"
image2Url = "./img2.jpg"
image1  = Image.open(image1Url)
image2  = Image.open(image2Url)

analyze_img1 = np.asarray(image1)
analyze_img2 = np.asarray(image2)

substr_img  = analyze_img1 - analyze_img2

last_image  = Image.fromarray(substr_img)

last_image.show()

Your question somewhat implies you are at the initial stages of this project and things are a bit vague, so I have just put together a few general ideas till you are further down the road.

  1. Consider moving to OpenCV as it has an abundance of useful functions and there are plenty of examples on Stack Overflow. Look for cv2.VideoCapture() to grab your camera stream and cv2.imshow() and cv2.waitKey() to display your frames. PIL is not very good for displaying dynamically changing images such as video and you will be much better served by cv2.imshow() . Also, you will find yourself converting PIL images to Numpy arrays to do processing, so you might as well use OpenCV and work directly with Numpy arrays already.

  2. If you want to detect changes in lighting, try converting your image to greyscale cv2.cvtColor(...BGR2GRAY...) and taking the mean with np.mean() . Try running your loop, which is looking about right, and just printing the mean in the Terminal at each iteration as you turn the lights and off. Then you can see how much it changes and work out a sensible threshold.

  3. As regards movement, you are probably not looking for movement at the pixel level, else you will detect the wind moving a leaf visible out of your window. So, you want large-scale changes. So, if your camera is 1080p (1920x1080), try resizing your image down to 16 pixels by 9 with cv2.resize(im, (16,9),... INTER_LINEAR ...) then you just have 144 pixels to quickly look at and compare between images. You can always scale these images back up to a side length of 300-400 cv2.resize(...NEAREST_NEIGHBOUR..) to display previous and current frames. Then look at the colour distances between each of the 144 image rectangles and see if any of them exceed a threshold you derive from your testing.

Try to develop your code a bit and get some experience, then come back and ask another question if you get stuck - questions, and answers, are free.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM