繁体   English   中英

使用 python 从 png 文件中读取文本时遇到困难

[英]Having a hard time reading a text from png file using python

图片

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

我的 Output: '\x0c'

预期 output:'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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM