使用 python 很难从 png 文件中读取文本

Having a hard time reading a text from png file using python

image

我很难从上图中提取文本 CHUBB。我尝试了几种图像预处理技术并使用 pytesseract 提取但没有成功。

我的输出:'\x0c'

预期输出:'CHUBB'

如有任何帮助,我们将不胜感激

我的尝试:

import pytesseract
img = cv2.imread('image1_1.png')

img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 

thresh1 = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, 

                                          cv2.THRESH_BINARY, 199, 5)

cv2.imshow('Adaptive Mean', thresh1)

# De-allocate any associated memory usage   

if cv2.waitKey(0) & 0xff == 27:  

    cv2.destroyAllWindows()
    
# Adding custom options
custom_config = r' --psm 3'
pytesseract.image_to_string(thresh1, config=custom_config)```

我认为问题是文字 CHUBB 对于图片来说太大了。 如果我们稍微减小尺寸或将其粘贴到更大的 canvas,那么 pytesseract 将正常工作

from PIL import Image
img = Image.open('test.png')  # load image
new_img = Image.new('RGB', (400, 400), color = 'white')  # create a larger canvas
new_img.paste(im=img, box=(100,100), mask=img)  # paste original CHUBB in the large image
text = pytesseract.image_to_string(new_img, lang='eng', config='--psm 12')  # OCR
print(text)  # CHUBB

仅供参考

for i in range(1,14):
    try:
        text = pytesseract.image_to_string(new_img, lang='eng',config=f"--psm {i}")  # OCR
        print('psm',i, text)
    except:
        pass

产量

psm 1 CHUBB

psm 3 CHUBB

psm 4 CHUBB

psm 5 0
u
J
I
U

psm 6 CHUBB

psm 7 CHUBB

psm 8 7

psm 9 CHUBB

psm 10 CHUBB

psm 11 CHUBB

psm 12 CHUBB

psm 13 7