问题
I have been trying to check if a pixel on the screen is changing. What do I need to do?
I have surfed the internet for a long time with no success. I have experimented with the code given on the net, and found out that my code is only giving data from the screen that was open when the code was run. ie, if the screen was white when the code was run, it will read pixels from the white screen, even though the screen color already changed.
from PIL import ImageGrab
px=ImageGrab.grab().load()
m=px[613,296]
print(m)
while 1:
if m!=px[613,296]:
m=px[613,296]
print(m)
I ran the code and started a video, I expected the values to keep changing but all I got was (255,255,255) (the white screen of the idle) I also tried to change the screen manually.
I tried runing the code without console and print the output without the while loop in a text file, I got correct values.But the task I need to complete needs to run the code several times to check if pixel updates. How should I accomplish this?
回答1:
This works for me:
#!/usr/bin/env python3
from PIL import ImageGrab
while True:
px=ImageGrab.grab().load()
m=px[613,296]
print(m)
I think it will be faster if you just grab one pixel though by specifying a bounding box like this so you only grab one pixel:
#!/usr/bin/env python3
from PIL import ImageGrab
while True:
screen=ImageGrab.grab(bbox=(613,296,614,297))
px = screen.load()
m=px[0,0]
print(m,screen.size)
来源:https://stackoverflow.com/questions/55501419/how-to-use-the-imagegrab-grab-load-function-or-any-other-function-to-get-pix