Search code examples
pythontesseract

Using Tesseract to recognize time from an image


I want to read the time from the image below using Tesseract. My code is:

text = pytesseract.image_to_string('test.png')
print(text)

The image is as below: enter image description here

But for some reason, this program doesn't work, and the text is always a space. Does anyone know how to solve this problem?


Solution

  • Use the flag config="--psm 6" and it should work. This specifies that you have a single uniform block of text. 7 also work which is for a single line of uniform text.

    print(pytesseract.image_to_string('test.png', config="--psm 6"))
    
    # prints "06-04-2021 11:27:17\n\x0c"
    

    See this other post for another example https://stackoverflow.com/a/66179528/15271127

    EDIT: Also see this website if you want more options https://muthu.co/all-tesseract-ocr-options/