python处理识别图片验证码
安装图片图像处理标准库PIL
- 32位windows系统下载链接:https://pypi.python.org/pypi/Pillow/2.1.0#id2
- 64位windows系统下载链接:https://pypi.python.org/pypi/Pillow/2.1.0#downloads
图片处理示例:
1 from PIL import Image 2 from pytesser import * 3 image = Image.open('7039.jpg') 4 print image_file_to_string('7039.jpg') 5 print image_to_string(image)
备注:如果出现报错ImportError: The _imaging C module is not installed,可能出现的原因下载错了版本,更改下安装64位的版本
图片识别
pytesser是谷歌OCR开源项目的一个模块,在python中导入这个模块即可将图片中的文字转换成文本,但是在pytesser模块中调用了tesseract,所以需要先安装tesseract
tesseract下载路径:https://bitbucket.org/3togo/python-tesseract/downloads/,选择合适的版本进行下载安装
- pytesser安装
- 下载路径:http://code.google.com/p/pytesser/ ,下载下来的模块包并不是传统的安装包,所以需要进行一系列的设置
- 解压文件夹,新建一个空的__init.py__文件
- 下载Tesseract OCR engine:http://code.google.com/p/tesseract-ocr/,解压后,将文件中的tessdata文件夹,复制至ptesser中进行替换原文件
- 复制pytesser至python安装目录的Libsite-packages,并且添加至环境变量中(如果觉得这一系列操作复杂可以直接将源码放到代码路径)
图片识别源码
1 from PIL import Image 2 from pytesser import * 3 image = Image.open('7039.jpg') 4 print image_file_to_string('7039.jpg') 5 print image_to_string(image)
文件示例 7039.jpg
可能遇到的问题及解决方案:
- raise IOError("cannot identify image file"),将脚本中的import Image改为from PIL import Image
- 将pytesser.py中的import Image改为from PIL import Image