环境:python 3.6.5
pillow 7.0.0
pytesseract-0.3.6
下面是一个简单的识别demo
import re
from PIL import Image
import pytesseract # https://tesseract-ocr.github.io/tessdoc/4.0-with-LSTM
if __name__ == '__main__':
img_path=r"E:\python_project\img\7364.png"
img=Image.open(img_path)
image = img.convert('L') # 转化为灰度图
threshold = t # 设定的二值化阈值
table = []
for i in range(256):
if i < threshold:
table.append(0)
else:
table.append(1)
image = image.point(table, '1')
pytesseract.pytesseract.tesseract_cmd = r"D:\Program Files\Tesseract-OCR\tesseract.exe"
result = pytesseract.image_to_string(image) # 图片转文字
print(result)
print(str(t))

网友评论