4

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

1 Answer 1

2

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()
Sign up to request clarification or add additional context in comments.

4 Comments

I will give it a try tonight. Thanks for the response!
Hi Nathancy, I got this "unsupported image object" error when I run the pytesseract command...
I think that I got it figured out. "thresh" is kind of an array format. I have to convert back to the image format with this "image_new = Image.fromarray(thresh)". Then this command will work "data = pytesseract.image_to_string(image_new, lang='eng', config='--psm 6')". It gives me the correct result now. Thank you!
Absolutely! Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.