有没有办法使用 mss 和 pytesseract 女巫保存和打开?



需要使用mss witchout保存和打开图像以"优化"此任务,这是我的代码,对不起,我的英语不好。

from PIL import Image
import pytesseract
import mss
import mss.tools
with mss.mss() as sct:
monitor = {'top': 171, 'left': 1090, 'width': 40, 'height': 17}
output = 'capture.png'.format(**monitor)
sct_img = sct.grab(monitor)
mss.tools.to_png(sct_img.rgb, sct_img.size, output=output)
text = pytesseract.image_to_string(Image.open('capture.png'))
print(text)

你介意使用Numpy吗?

import mss
import numpy
import pytesseract

monitor = {'top': 171, 'left': 1090, 'width': 40, 'height': 17}
with mss.mss() as sct:
im = numpy.array(sct.grab(monitor), dtype=numpy.uint8)
im = numpy.flip(im[:, :, :3], 2)  # BGRA -> RGB conversion
text = pytesseract.image_to_string(im)
print(text)

一个简单的一次性时间比较给了我:

MSS.tools + PIL: 0.00988 s
MSS + Numpy    : 0.00222 s

最新更新