先上代码:

import pytesseract
from PIL import Image

# 打开图片
image = Image.open('011.png')
# 转为灰度图片
imgry = image.convert('L')
# 二值化,采用阈值分割算法,threshold为分割点,根据图片质量调节
threshold =165 #150
table = []
for j in range(256):
    if j < threshold:
        table.append(0)
    else:
        table.append(1)

temp = imgry.point(table, '1')

# OCR识别:lang指定中文,--psm 6 表示按行识别,有助于提升识别准确率
text = pytesseract.image_to_string(temp, lang="chi_sim+eng", config='--psm 6')

# 打印识别后的文本
print(text)

除了装pytesseract和PIL库外,还需要:

1)安装Tesseract-OCR  ,安装完成后加入到环境变量里面;

2)安装支持语言种类如chi,sim ,eng

3)每次使用替换掉图片位置和名称,可以通过调整threshold的值来提高图片的识别率

Logo

腾讯云面向开发者汇聚海量精品云计算使用和开发经验,营造开放的云计算技术生态圈。

更多推荐