How to make image more contrast, grayscale then get all characters exactly with PIL and pytesseract?

Viewed 918

PLease download the attatchment here and save it as /tmp/target.jpg.

enter image description here
You can see that there are 0244R in the jpg,i extract string with below python code:

from PIL import Image
import pytesseract
import cv2
filename = "/tmp/target.jpg"
image = cv2.imread(filename)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
ret, threshold = cv2.threshold(gray,55, 255, cv2.THRESH_BINARY)
print(pytesseract.image_to_string(threshold))

What I get is

0244K

The right string is 0244R,how to make image more contrast, grayscale then get all characters exactly with with PIL and pytesseract? Here is the webpage which generate the image :

http://www.crup.cn/ValidateCode/Index?t=0.14978241776661583

1 Answers

If you apply adaptive-thresholding and bitwise-not operations to the input image, the result will be:

enter image description here

Now if you remove the special characters like (dot, comma, etc..)

txt = pytesseract.image_to_string(bnt, config="--psm 6")
res = ''.join(i for i in txt if i.isalnum())
print(res)

Result will be:

O244R

Code:


import cv2
import pytesseract

img = cv2.imread("Aw6sN.jpg")
gry = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thr = cv2.adaptiveThreshold(gry, 255, cv2.ADAPTIVE_THRESH_MEAN_C,
                            cv2.THRESH_BINARY_INV, 23, 100)
bnt = cv2.bitwise_not(thr)
txt = pytesseract.image_to_string(bnt, config="--psm 6")
res = ''.join(i for i in txt if i.isalnum())
print(res)
Related