pillow、pytesseract、tesseract-ocr
前面2个可以直接pip安装,tesseract-ocr需要去下载安装包(直接网上搜,很多)
安装完运行下py代码
import pytesseract
from PIL import Image
img=Image.open('./ver.jpg')
print (pytesseract.image_to_string(img, lang='chi_sim'))
出现错误: FileNotFoundError: [WinError 2] 系统找不到指定的文件。
根据网上资料,更改 pytesseract.py 内容
tesseract_cmd = 'tesseract' 改成绝对路径:
tesseract_cmd = 'C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
再次运行代码
出现错误:
pytesseract.pytesseract.TesseractError: (1, 'Error opening data file C:\\Program Files (x86)\\Tesseract-OCR\\chi_sim.traineddata Please make sure the TESSDATA_PREFIX environment variable is set to your "tessdata" directory. Failed loading language \'chi_sim\' Tesseract couldn\'t load any languages! Could not initialize tesseract.')
显示没把 TESSDATA 加入环境变量
添加完还是出现上面的错误
运行 dos tesseract是可以运行的
但是在py代码里面就不可以了,什么原因呢??????
再仔细观察错误,原来少了 chi_sim 的语言包,修改下源码
import pytesseract
from PIL import Image
img=Image.open('./ver.jpg')
print (pytesseract.image_to_string(img))
总结:原来的源码是从别的网站上拷过来,没留言没有 chi_sim 中文语言包,导致出错,一直以为是 环境变量问题
,下次要仔细点才行