一.问题背景
python使用tesseract,已经使用命令行安装了pytesseract 运行读取验证码测试代码:
#coding=utf-8
from PIL import Image
import pytesseract
#非规则性
image = Image.open("D:\D1\TestDevelopment\data\SeleniumData\shot2.png")
text = pytesseract.image_to_string(image)
print("非规则性图片读取----->",text)
#非规则性
image1 = Image.open("D:\D1\TestDevelopment\data\SeleniumData\shot3.webp")
text1 = pytesseract.image_to_string(image1)
print("规则性图片读取----->",text1)
完整报错信息如下:
Traceback (most recent call last):
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\pytesseract\pytesseract.py", line 252, in run_tesseract
proc = subprocess.Popen(cmd_args, **subprocess_args())
File "D:\D1\IDEs\Python3.8.5\lib\subprocess.py", line 854, in __init__
self._execute_child(args, executable, preexec_fn, close_fds,
File "D:\D1\IDEs\Python3.8.5\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File ".\chapter2\start_browser2-14.py", line 7, in <module>
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\pytesseract\pytesseract.py", line 413, in image_to_string
return {
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\pytesseract\pytesseract.py", line 416, in <lambda>
Output.STRING: lambda: run_and_get_output(*args),
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\pytesseract\pytesseract.py", line 284, in run_and_get_output
run_tesseract(**kwargs)
File "D:\D1\IDEs\Python3.8.5\lib\site-packages\pytesseract\pytesseract.py", line 256, in run_tesseract
raise TesseractNotFoundError()
pytesseract.pytesseract.TesseractNotFoundError: tesseract is not installed or it's not in your PATH. See README file for more
information.
二.问题解决
经在网上查询,tesseract的使用还需要安装Pillow,tesseract-ocr,由于之前已经安装过pillow,故只需要安装tesseract-ocr
具体步骤如下: 1.安装tesseract https://github.com/tesseract-ocr/tesseract
2.修改1目录下pytesseract.py文件,指定tesseract.exe安装路径
tesseract_cmd = r'D:\\D1\\python\\tesseract\\Tesseract-OCR\\tesseract.exe'
3.增加系统环境变量
编辑path系统环境变量
4.编辑环境变量后重新打开cmd窗口,输入 5.最后重新运行代码,能成功运行输出 参考文章: https://blog.csdn.net/JavaBigData/article/details/108030678
|