Python识别验证码主要依靠以下三个库来实现的。
1.Pillow 安装
Python3以后已经不支持PIL,所以要是用pillow,使用方法跟PIL一样。
安装:pip install pillow
2.安装tesseract-ocr
下载地址:https://github.com/tesseract-ocr/tesseract/wiki/4.0-with-LSTM#400-alpha-for-windows
Tesseract:是一个开源的光学字符识别(OCR)引擎,可在Apache 2.0许可下使用。它可以直接使用,或者(对于程序员)使用API从图像中提取打印的、手写的或打印的文本。它支持多种语言。
Tesseract没有内置的GUI,但是在3rdParty页面上有几个可用的GUI。
环境变量配置:
2.1 将安装目录配置到环境变量和用户变量的path里面。

2.2 验证tesserat环境变量配置是否成功
打开CMD,输入tesseract -v。出现版本信息,说明环境变量配置好了

3.pytessract安装
直接使用pip install pytesseract安装即可,或者使用easy_install pytesseract
用来连接tessearct进行验证码识别
4.简单的验证码识别例子

from pytesseract import image_to_string
from PIL import Image
img = Image.open(r"GetValidateCode.jpg")
# print(img)
print(image_to_string(img))

5.踩坑无数的小白
ok,这样就完成了一个简单的验证码识别。快去尝试一下吧。
当然过程可能不会这么的一帆风顺,可能会出现下面的错误。不要慌

让小白来带你一起踩坑。
首先找到你安装在Python目录下(我这里是Python34)的pytesseract目录里面的pytesseract.py文件

然后打开它。修改下面的路径(我这里用的Notepad++)


网友评论
Traceback (most recent call last):
File "yanzhengma.py", line 5, in <module>
print(image_to_string(img))
File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 193, in image_to_string
return run_and_get_output(image, 'txt', lang, config, nice)
File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 140, in run_and_get_output
run_tesseract(**kwargs)
File "/usr/local/lib/python2.7/dist-packages/pytesseract/pytesseract.py", line 111, in run_tesseract
proc = subprocess.Popen(command, stderr=subprocess.PIPE)
File "/usr/lib/python2.7/subprocess.py", line 390, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1025, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
大佬出现这种问题是什么情况?