如何使用ImageGrab.grab().load()函数或任何其他函数获取像素更新? [英] How to use the ImageGrab.grab().load() function or any other function to get pixel updates?

查看:105
本文介绍了如何使用ImageGrab.grab().load()函数或任何其他函数获取像素更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试检查屏幕上的像素是否正在更改.我该怎么办?

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)

我运行了代码并开始播放了一段视频,我希望值会不断变化,但我得到的只是(255,255,255)(空闲的白色屏幕),我也尝试过手动更改屏幕.

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.

我尝试在没有控制台的情况下运行代码,并在文本文件中不使用while循环的情况下打印输出,我得到了正确的值,但是我需要完成的任务需要多次运行代码以检查像素是否更新.我应该如何做到这一点?

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?

推荐答案

这对我有用:

#!/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)

这篇关于如何使用ImageGrab.grab().load()函数或任何其他函数获取像素更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆