Why does tesseract fail to read text off this simple image?

hegash picture hegash · Jan 18, 2019 · Viewed 8.8k times · Source

I have read mountains of posts on pytesseract, but I cannot get it to read text off a dead simple image; It returns an empty string.

Here is the image:

TestImage

I have tried scaling it, grayscaling it, and adjusting the contrast, thresholding, blurring, everything it says in other posts, but my problem is that I don't know what the OCR wants to work better. Does it want blurry text? High contrast?

Code to try:

import pytesseract
from PIL import Image

print pytesseract.image_to_string(Image.open(IMAGE FILE))

As you can see in my code, the image is stored locally on my computer, hence Image.open()

Answer

Davide Fiocco picture Davide Fiocco · Jan 19, 2019

Trying something along the lines of

import pytesseract 
from PIL import Image 
import requests 
import io

response = requests.get('https://i.stack.imgur.com/J2ojU.png') 
img = Image.open(io.BytesIO(response.content))
text = pytesseract.image_to_string(img, lang='eng', config='--psm 7')

print(text)

with --psm values equal or larger than 6 did yield "Gm" for me.

If the image is stored locally (and in your working directory), just drop the response variable and change the definition of text with the lines

image_name = "J2ojU.png" # or whatever appropriate
text = pytesseract.image_to_string(Image.open(image_name), lang='eng', config='--psm 7')