python-imaging-librarypython-tesseractpython-mss

Is there a way to use mss and pytesseract witchout saving and open?


Need to use mss witchout saving and open images in order to "optimize" this task here is mine code and sorry my bad english.

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)

Solution

  • Do you mind using 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)
    

    A simple one shot time comparison gives me:

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