使用python以及工具包进行简单的验证码识别
识别数字验证码
首先我们准备素材,4张验证码图片如下:
第一步:
打开图像。
im = Image.open('temp1.jpg')
第二步:
把彩色图像转化为灰度图像。彩色图像转化为灰度图像的方法很多,这里采用RBG转化到HSI彩色空间,采用I分量。
imgry = im.convert('L')
灰度看起来是这样的
第三步:
需要把图像中的噪声去除掉。这里的图像比较简单,直接阈值化就行了。我们把大于阈值threshold的像素置为1,其他的置为0。对此,先生成一张查找表,映射过程让库函数帮我们做。
threshold = 140 table = [] for i in range(256): if i < threshold: table.append(0) else:
阈值为什么是140呢?试出来的,或者参考直方图。
映射过程为
out = imgry.point(table,'1')
此时图像看起来是这样的
第四步:
将图片中的字符转化为文本。采用pytesser 中的image_to_string函数
text = image_to_string(out)
第五步:
优化。根据观察,验证码中只有数字,并且上面的文字识别程序经常把8识别为S。因此,对于识别结果,在进行一些替换操作。
#由于都是数字 #对于识别成字母的 采用该表进行修正 rep={'O':'0', 'I':'1','L':'1', 'Z':'2', 'S':'8'
for r in rep: text = text.replace(r,rep[r])
好了,text中为最终结果。
7025
0195
7039
6716
0195
7039
6716
完整代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
import Image import ImageEnhance import ImageFilter import sys from pytesser import * # 二值化 threshold = 140 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) #由于都是数字 #对于识别成字母的 采用该表进行修正 rep={'O':'0', 'I':'1','L':'1', 'Z':'2', 'S':'8' }; def getverify1(name): #打开图片 im = Image.open(name) #转化到亮度 imgry = im.convert('L') imgry.save('g'+name) #二值化 out = imgry.point(table,'1') out.save('b'+name) #识别 text = image_to_string(out) #识别对吗 text = text.strip() text = text.upper(); for r in rep: text = text.replace(r,rep[r]) #out.save(text+'.jpg') print text return text getverify1('v1.jpg') getverify1('v2.jpg') getverify1('v3.jpg') getverify1('v4.jpg')
本文来自博客园,作者:''竹先森゜,转载请注明原文链接:https://www.cnblogs.com/zhuminghui/p/9379454.html