How to use the ImageGrab.grab().load() function or any other function to get pixel updates?

南楼画角 提交于 2019-12-09 03:48:14

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!