Search code examples
pythonpython-imaging-librarypython-tesseract

Why is the text not recognized from this image?


The text isn't properly recognized from the following image, although it's working for other images:

https://ibb.co/StWYzkP

from PIL import Image
from pytesseract import pytesseract

pytesseract.tesseract_cmd = (r'C:\Users\SPT\AppData\Local\Programs\Tesseract-OCR\tesseract.exe')

img = Image.open(r'C:\Users\SPT\Desktop\Bot\bot\savedimage.png')

x = pytesseract.image_to_string(img)
print(x)

Solution

  • Keep Improving the quality of the output nearby, when using tesseract.

    Your input is some three-channel RGB image. First try, even before binarization, would be to convert to single-channel grayscale:

    from PIL import Image
    from pytesseract import pytesseract
    
    img = Image.open('b8JEG.png').convert('L')
    
    x = pytesseract.image_to_string(img)
    print(repr(x))
    # '40\n\n'
    

    As you can see, that already helped.

    Alternatively, have a look at the page segmentation mode section. Mode 6 seems reasonable here:

    from PIL import Image
    from pytesseract import pytesseract
    
    img = Image.open('b8JEG.png')
    
    x = pytesseract.image_to_string(img, config='-psm 6')
    print(repr(x))
    # '40\n\n'
    

    That also does the job.

    ----------------------------------------
    System information
    ----------------------------------------
    Platform:      Windows-10-10.0.16299-SP0
    Python:        3.9.1
    PyCharm:       2021.1.1
    Pillow:        8.2.0
    pytesseract:   4.00.00alpha
    ----------------------------------------