Search code examples
pythonimageimage-processingocrpython-tesseract

python pytesseract.image_to_string can't read text in image


I am using python3.7 and Tesseract-OCR version 5 on my Windows 10 box. I have pictures containing the numbers. However, despite that it is super clear to the human eyes, the Tesseract can't extract them correctly. Some give me a couple of correct readings. Some don't return anything at all. The attached one is the extreme case that nothing is returned...

text = pytesseract.image_to_string(n)
print(text) -> returns nothing

I read that I must change the DPI to 300 for Tesseract to read it correctly. Could you show me the best way to do it? I googled but I couldn't find a straight forward way to do it. Thanks!

Input image

enter image description here


Hi Nathancy, here is the "unsupported image object" error I got when I run the pytesseract command

>>> data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 309, in image_to_string
}[output_type]()
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 308, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 208, in run_and_get_output
temp_name, input_filename = save_image(image)
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 121, in save_image
image = prepare(image)
  File "C:\Python37\lib\site-packages\pytesseract\pytesseract.py", line 113, in prepare
raise TypeError('Unsupported image object')
TypeError: Unsupported image object

Solution

  • Here's a quick example performing a bit of preprocessing using OpenCV:

    enter image description here

    Result from Pytesseract OCR:

    55 58 6 25 41 1
    

    Code

    import cv2
    import pytesseract
    
    pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
    
    # Load image, grayscale, Otsu's threshold
    image = cv2.imread('1.jpg')
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    thresh = 255 - cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]
    
    # Blur and perform text extraction
    thresh = cv2.GaussianBlur(thresh, (3,3), 0)
    data = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')
    print(data)
    
    cv2.imshow('thresh', thresh)
    cv2.waitKey()