Search code examples
pythonpython-tesseract

How to read text from only a portion of the image with pytesseract


I'm trying to read the 213 from this image but i cant even get pytesseract to read everything Here is my best effort code:

import cv2
import pytesseract

img = cv2.imread('gamepictures/text.png')  # Load the image
img = cv2.cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # convert to grey
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 15)
txt = pytesseract.image_to_string(img, config='--psm 6')
print(txt)
cv2.imshow("", img)
cv2.waitKey(0)

I have been trying to change the treshholding algorithm i even tried with canny, but i can't get it to work. So my questions are how can i read everything?

And how can i only read the 213

image


Solution

  • Something like this works:

    import cv2
    import pytesseract
    
    img = cv2.imread('gamepictures/text.png')  # Load the image
    img = img[98:190,6:149,:]
    img = cv2.cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)  # convert to grey
    img = cv2.GaussianBlur(img, (5, 5), 3)
    img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 7, -2)
    
    txt = pytesseract.image_to_string(img, config='--psm 10 -c tessedit_char_whitelist=0123456789')
    print(img.shape)
    print(txt)
    cv2.imshow("", img)
    cv2.waitKey(0)
    

    Basically I just sliced the image and played around with the parameters a bit. The GaussianBlur is there to make the image more continuous.

    The -c tessedit_char_whitelist=0123456789 is optional and just makes sure that only numbers are read.