完整代码如下:
from PIL import Image
# 加载PIL模块中的Image模组
import pytesseract
# 加载pytesseract模块
img=Image.open('xxxx.jpeg')
# 创建将要识别的图片对象img
# xxxx.jpeg为你将要打开的图片路径
txt=pytesseract.image_to_string(img,lang='chi_sim')
# pytesseract.image_to_string()函数进行识别
# 创建将要识别后的文字对象txt
# lang='chi_sim' 参数设置为中文文字
print(txt)
# 终端显示
- 需要加载两个模块:pytesseract和PIL
- 需要安装识别引擎:tesseract-ocr