我想知道是否可以监视屏幕上的文本变化。以这个健康栏为例:
这将输出:
护盾:100 生命值:100
护盾:82 生命值:100
总的来说,我想要的输出是这样的:
护盾:100 生命值:100
护盾:100 生命值:100
护盾:100 生命值:99
是的,绝对有可能。您可以使用PIL.ImageGrab截取屏幕该部分的屏幕截图,并使用pytesseract OCR 将其转换为值
你可以使用这样的东西:
#Import the required libraries
import PIL.ImageGrab
import pytesseract
#INPUT the screen pixel coordinates of a box that surrounds the two numerical values (health and shields)
cropRect = (x1,y1,x2,y2)
#Grab the screenshot, crop it to the box that you input, and then use OCR to convert the values on the image to a string
values = pytesseract.image_to_string(PIL.ImageGrab.grab().crop(cropRect))
#Use the OCR output to extract the values that you need (using normal string manipulation)
shields = int(values[:values.find("/n")])
health = int(values[values.find("/n")+1:])
print(f"Shields: {shields} Health: {health}")
Run Code Online (Sandbox Code Playgroud)
您必须检查 OCR 输出,看看可以使用哪个字符来分割“values”变量(有时可以用“\n”,有时用“\t”或“/”分割)。您可以使用 print(values) 检查以找到正确的字符串分隔符。
如果您想连续监视这些值,请将其放入 while(True) 循环中(或放入它自己的线程中)。像这样的事情:
import PIL.ImageGrab
import pytesseract
import time
cropRect = (x1,y1,x2,y2)
while(True):
values = pytesseract.image_to_string(PIL.ImageGrab.grab().crop(cropRect))
shields = int(values[:values.find("/n")])
health = int(values[values.find("/n")+1:])
print(f"Shields: {shields} Health: {health}")
time.sleep(1)
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
| 归档时间: |
|
| 查看次数: |
2454 次 |
| 最近记录: |