1

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)
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
Siewdass Sf
  • 169
  • 1
  • 10

1 Answers1

0

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
Tiger-222
  • 6,677
  • 3
  • 47
  • 60
  • Its not working for my, i tested it in debian stable with python3 + numpy + mss + pytesseract im new with this libraries. – Siewdass Sf May 29 '18 at 17:36
  • and i don't have any errors but don't know why its not working – Siewdass Sf May 29 '18 at 17:49
  • Can you explain this: dtype=numpy.uint8) im = numpy.flip(im[:, :, :3], 2)? i need to understand X.X im new with numpy u.u – Siewdass Sf May 29 '18 at 18:19
  • I just added a comment for the `flip()` usage. By default, MSS returns an array with pixels in BGRA order; the flip will rearrange into RGB. `dtype` it is the type of each value in the array, it is the default value but interestingly it is a little faster when specifying it directly. – Tiger-222 May 29 '18 at 18:21
  • Is it okay for you? – Tiger-222 May 30 '18 at 09:56