持续监视屏幕上的文本变化
Continuously monitor the screen for changes in text
我想知道是否可以监视屏幕上的文本变化。例如,拿这个健康栏:
这将输出:
Shield: 100
Health: 100
或者如果它看到这个:
它将 return:
Shield: 82
Health: 100
总的来说,我想要的输出是这样的:
Shield: 100
Health: 100
Shield: 100
Health: 100
Shield: 100
Health: 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}")
您必须检查 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)
希望对您有所帮助
我想知道是否可以监视屏幕上的文本变化。例如,拿这个健康栏:
这将输出:
Shield: 100 Health: 100
或者如果它看到这个:
它将 return:
Shield: 82 Health: 100
总的来说,我想要的输出是这样的:
Shield: 100 Health: 100
Shield: 100 Health: 100
Shield: 100 Health: 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}")
您必须检查 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)
希望对您有所帮助