【问题标题】:Is there a way to use mss and pytesseract witchout saving and open?有没有办法在不保存和打开的情况下使用miss和tesseract?
【发布时间】:2018-05-29 15:50:57
【问题描述】:

需要使用 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)

【问题讨论】:

    标签: python-imaging-library python-tesseract python-mss


    【解决方案1】:

    你介意使用 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
    

    【讨论】:

    • 我没有任何错误,但不知道为什么它不起作用
    • 你能解释一下吗:dtype=numpy.uint8) im = numpy.flip(im[:, :, :3], 2)?我需要用 numpy u.u 了解 X.X im new
    • 我刚刚为flip() 用法添加了评论。默认情况下,MSS 返回一个像素按 BGRA 顺序排列的数组;翻转将重新排列为 RGB。 dtype 是数组中每个值的类型,是默认值,有趣的是直接指定会快一点。
    • 你没事吧?
    猜你喜欢
    • 1970-01-01
    • 2021-09-24
    • 2019-04-26
    • 1970-01-01
    • 2012-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-23
    相关资源
    最近更新 更多