在使用 pytesseract.image_to_string()函数时,报错如下:
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your path
原因是没有找到Tesseract-OCR\tesseract.exe的位置,如果没有Tesseract-OCR,则需要手动安装,下载完安装即可,默认路径C:\Program Files (x86)\下面。
http://digi.bib.uni-mannheim.de/tesseract/tesseract-ocr-setup-4.00.00dev.exe
方案一:
打开pytesseract源码,查找tesseract_cmd = ‘tesseract’
发现上面有一行注释 # CHANGE THIS IF TESSERACT IS NOT IN YOUR PATH, OR IS NAMED DIFFERENTLY,由于 TESSERACT 所在目录不同,需要手动更改目录
此行修改为:
tesseract_cmd = r’C:\Program Files (x86)\Tesseract-OCR\tesseract.exe’
方案二:
不打开pytesseract源码,在用pytesseract.image_to_string()函数之前,增添一条路径语句:pytesseract.pytesseract.tesseract_cmd =‘c://Program Files (x86)//Tesseract-OCR//tesseract.exe’
import pytesseract
from PILimport Image
pytesseract.pytesseract.tesseract_cmd ='c:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
tessdata_dir_config ='--tessdata-dir "c:/Program Files (x86)/Tesseract-OCR//tessdata"'
text = pytesseract.image_to_string(picture)
print("验证码为:", text)