Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python : How to detect any changes in the screen

Tags:

python

is there any code that can detect if any pixel color changes ?

what i want to do is to detect any change in a region if pixels

so if the color of any pixel in that region changed , it alerts me .

sorry for my bad English .

like image 765
user3101703 Avatar asked Oct 31 '25 03:10

user3101703


2 Answers

To find the exact difference between consecutive screenshots:

#!/usr/bin/env python
from PIL import ImageChops # $ pip install pillow
from pyscreenshot import grab # $ pip install pyscreenshot

im = grab()
while True: # http://effbot.org/zone/pil-comparing-images.htm
    diff = ImageChops.difference(grab(), im)
    bbox = diff.getbbox()
    if bbox is not None: # exact comparison
        break

print("bounding box of non-zero difference: %s" % (bbox,))
# superimpose the inverted image and the difference
ImageChops.screen(ImageChops.invert(im.crop(bbox)), diff.crop(bbox)).show()
input("Press Enter to exit")
like image 187
jfs Avatar answered Nov 02 '25 15:11

jfs


An intuitive solution could be the folllowing:

1) Take the bytes of the region you want to check for changes
2) Convert them to a string of bytes
3) Use an hash function from the hashlib module
4) Compare the obtained hash with the previous one
   (if they're different something has changed)

This approach detect any change also in light conditions, so may not be what you want. But this truly detect ANY change in the portion of the image.

There are also some libraries binding like PyOpenCV that can do this and a lot more.

like image 32
Giova Avatar answered Nov 02 '25 17:11

Giova



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!