Pytesseract Wrong Number

I have a problem with the recognition, that some of my input images that are visibly a 1 turn into a 4 after the .image_to_string() command.

My input image is this:
unedited img

I then run some preprocessing steps over it (greyscale, thresholding with otsu, and enlarge the picture) leading to this: preprocessed img

I also tried dilating the picture with no improvement in the output changing.

After running:

    custom_config = "-c  tessedit_char_whitelist=0123456789LV --psm 13"
    pytesseract.image_to_string(processed_img, config=custom_config)

The final result is a String Displaying: 4LV♀ and I don’t understand what I can change to get a 1 instead of the 4.

Thanks in advance for your time.

Answer

The ♀ or nx0c is because you need custom_config = "-c page_separator=''" as the config because for some reason it adds it as the page separator. you don’t need anything else in your config.

To get your number is to do with the processing, mainly to do with the size. However this code i found works best.

import pytesseract
from PIL import Image
import cv2
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
import numpy as np 

imagepath = "./Pytesseract Wrong Number/kD3Zy.jpg" 
read_img = Image.open(imagepath)

# convert PIL image to cv2 image locally
read_img = read_img.convert('RGB')
level_img = np.array(read_img)
level_img = level_img[:, :, ::-1].copy()

# convert to grayscale
level_img = cv2.cvtColor(level_img, cv2.COLOR_RGB2GRAY)
level_img, img_bin = cv2.threshold(level_img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
level_img = cv2.bitwise_not(img_bin)
kernel = np.ones((2, 1), np.uint8)

# make the image bigger, because it needs at least 30 pixels for the height for the characters
level_img = cv2.resize(level_img,(0,0),fx=4,fy=4, interpolation=cv2.INTER_CUBIC)
level_img = cv2.dilate(level_img, kernel, iterations=1)

# --debug--
#cv2.imshow("Debug", level_img)
#cv2.waitKey()
#cv2.destroyAllWindows
#cv2.imwrite("1.png", level_img)

custom_config = "-c page_separator=''"
level = pytesseract.image_to_string(level_img, config=custom_config)
print(level)

if you want to save it uncomment #cv2.imwrite("1.png", level_img)