您的位置:首页 > 编程语言 > Python开发

python的验证码识别.md

2015-11-23 14:40 621 查看

1. 三个文件:

pytesseract

PIL

tesseract

上面两个直接pip,核心部件tessract是一个exe包,大概是是pytesseract调用tesseract,再通过PIL库对验证码进行识别。

2. bug修复

网上很多人的博客都讲述了基于pytesseract来对验证码的识别,但是按照他们的方法运行,却会报如下错

Traceback (most recent call last):

File “**.py”, line , in

vcode = pytesseract.image_to_string(image)

File “C:\Python27\lib\site-packages\pytesseract\pytesseract.py”, line 143, in image_to_string

if len(image.split()) == 4:

File “C:\Python27\lib\site-packages\PIL\Image.py”, line 1497, in split

if self.im.bands == 1:

AttributeError: ‘NoneType’ object has no attribute ‘bands’

问题应该出现在pytesseract.py的143和Image.py的1497行。进Python27\Lib\site-packages\PIL,将split函数换成如下

def split(self):
"Split image into bands"
self.load()
if self.im.bands == 1:
ims = [self.copy()]
else:
ims = []
#self.load()
for i in range(self.im.bands):
ims.append(self._new(self.im.getband(i)))
return tuple(ims)


这样就应该好了。问题应该是self.load()这个函数出错了,注释掉即可。

3. demo

#coding:utf-8
import requests
from PIL import Image
import pytesseract
import cStringIO
code_url = 'http://self.nwpu.edu.cn:8080/selfservice/common/web/verifycode.jsp'

file = cStringIO.StringIO(requests.get(code_url).content)
image = Image.open(file)
print pytesseract.image_to_string(image)


运行结果:4070

如demo,可以将http://self.nwpu.edu.cn:8080/selfservice/common/web/verifycode.jsp 的验证码识别

4. 参考资料

http://blog.bear2.cn/

http://range.pw/?p=16
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: