如何利用pytesser识别简单图形验证码?( 二 )


四、利用pytesser模块实现识别
pytesser是谷歌OCR开源项目的一个模块 , 在python中导入这个模块即可将图片中的文字转换成文本 。
pytesser%20调用了%20tesseract 。在python中调用pytesser模块 , pytesser又用tesseract识别图片中的文字 。
4.1%20pytesser安装
先安装PIL , 再安装pytesser , 并将其解压到项目代码下 , 或者解压到python安装目录的Libsite-packages下 , 并将其添加到path环境变量中 , 不然在导入模块时会出错 。
下载Tesseract%20OCR%20engine , 下载后解压 , 找到tessdata文件夹 , 用其替换掉pytesser解压后的tessdata文件夹即可 。
另外如果现在都是从PIL库中运入Image , 没有使用Image模块 , 所以需要把pytesser.py中的import%20Image改为from%20PIL%20import%20Image,%20其次还需要在pytesser文件夹中新建一个__init__.py的空文件 。
4.2%20调用pytesser识别
pytesser提供了两种识别图片方法 , 通过image对象和图片地址 , 代码判断如下:
from%20PIL%20import%20Image
from%20pytesser%20import%20pytesser
image%20=%20Image.open('7039.jpg')
print pytesser.image_file_to_string('7039.jpg')
print pytesser.image_to_string(image)
同时pytesser还支持其他语言的识别 , 比如中文 。




推荐阅读