1

I was wondering how to make python detect changes on part of my screen and do something if there was a change. I don't need it to detect a specific change I just need it to detect any pixel change.

I've looked in many places but I can't find anything for my needs.

1 Answer 1

1

This answer is similar in concept to Hank's answer but requires less code.

You can use pyautogui to take screenshots of your screen (which will require installing using pip or conda).

What the code does is take and save an initial first screenshot, and then begin a loop. Each time, the loop takes a new screenshot, compares the two screenshots, and then sets the 1st screenshot as the 2nd one. This way we can take one screenshot every time instead of two and make the script faster and less process consuming.

We can then use read() to read the pixel values of both images and check for differences. Because we use the same method to take them, in this case you won't need to worry about quality or compression.

import pyautogui

region_of_screen = (0, 0, 300, 300) #You can change the place of the screen you want to monitor here

croppedscreen = pyautogui.screenshot(region=region_of_screen).save('./screen1.png') #You can also use `pyautogui.screenshot()` with no `region` to capture the entire screen instead

while True: # While loop to constantly check

    croppedscreen2 = pyautogui.screenshot(region=region_of_screen) #Take the 2nd screenshot
    croppedscreen2.save('./screen2.png') #Save 2nd screenshot

    if open("screen1.png","rb").read() == open("screen2.png","rb").read(): #Compare pixel values of both images
        print('No changes detected')
    else:
        print('Changes detected!')

    croppedscreen = croppedscreen2 #Since we're detecting changes between each screenshot, we can save resources by using the 2nd screenshot as our 1st one and taking a new screenshot to compare it with.
    croppedscreen.save('screen1.png')
Sign up to request clarification or add additional context in comments.

5 Comments

thanks, it works well but do you know how I stop the loop with out closing the program, in case you need to know I'm using VS code
@nathansitter I use VS code too! What do you exactly mean by "stop the loop"? Also, "closing the program"? Are there any preference or conditions you would like?
when the program is running and constantly running the process, is there a way to make the program stop running?
You could use a keyboard interrupt inside of the VSCode terminal (Ctrl+C on Mac, keybinds can be found here: code.visualstudio.com/docs/getstarted/keybindings). If you want the code to exit the loop but still keep running, you could put it in a try and except block and use a keyboard interrupt. Here's some info about try and except blocks: w3schools.com/python/python_try_except.asp
ok, thanks you .

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.