#!C:/Python27
#coding=utf-8
"""
1、截屏,获取验证码图片;
2、裁剪验证码为4个小图片;
3、把4张图片放大为255*255像素图
4、拿每块小图片去模型目录找相似的图片*4次;
5、组装成字符串--->4位验证码
"""
import os ,sys
import fnmatch
import re,time
import pytesseract
from pytesser import *
from PIL import Image,ImageDraw
import urllib,random
def GetVerficode(): #下载验证码图片
j = 1000
for i in range(1,2):
url = 'https://ssom-cas.sfbest.com/cas/vcode?a=0.9529792121745766'
print "download", i
file("./code/%d.jpg" % j, "wb").write(urllib.urlopen(url).read())
j += 1
def GetImg():
ImgPath = (".//code//9911.jpg")
def CutCrop():#分隔验证图片
"""global data 全局变量在整个函数中有效"""
ImgPath = (".//code//")
j = 1000
for f in os.listdir(ImgPath):
if f.endswith(".jpg"):
print f
img = Image.open(ImgPath+f).convert('L')
print img.size
w, h = img.size
#rowheight = h // rownum
#colwidth = w // colnum
#imgry.show()
for i in range(4):
x = 10 + i*24 #验证码的x,y坐标 10+i*24 y=6 (x-4, y,x+6, y+14)
y = 8
img.crop((x-4, y,x+8, y+14)).save("font/%d.bmp" % j)
print "j=",j
j += 1
def fixed_size():
"""按照固定尺寸放大处理4图片"""
dirpath =(".//font//")
dirSave =(".//BigImg//")
j = 10000
for imgfile in os.listdir(dirpath):
print imgfile
im = Image.open(dirpath+imgfile)
#im.show()
size = (256, 256)
im2 =im.resize(size).convert('RGB')
out = im2.resize(size,Image.ANTIALIAS)
out.save(dirSave+'%d.bmp' % j)
print u"\n按固定尺寸放大*4张图片,处理已完成"
j += 1
"""对比图片方法开始"""
def getGray(image_file):
tmpls=[]
for h in range(0, image_file.size[1]):#h
for w in range(0, image_file.size[0]):#w
tmpls.append( image_file.getpixel((w,h)) )
return tmpls
def getAvg(ls):#获取平均灰度值
return sum(ls)/len(ls)
def getMH(a,b):#比较100个字符有几个字符相同
dist = 0;
for i in range(0,len(a)):
if a[i]==b[i]:
dist=dist+1
return dist
def getImgHash(fne):
image_file = Image.open(fne) # 打开
image_file=image_file.resize((12, 12))#重置图片大小我12px X 12px
image_file=image_file.convert("L")#转256灰度图
Grayls=getGray(image_file)#灰度集合
avg=getAvg(Grayls)#灰度平均值
bitls=''#接收获取0或1
#除去变宽1px遍历像素
for h in range(1, image_file.size[1]-1):#h
for w in range(1, image_file.size[0]-1):#w
if image_file.getpixel((w,h))>=avg:#像素的值比较平均值 大于记为1 小于记为0
bitls=bitls+'1'
else:
bitls=bitls+'0'
return bitls
'''''
m2 = hashlib.md5()
m2.update(bitls)
print m2.hexdigest(),bitls
return m2.hexdigest()
'''
"""对比图片方法结束"""
def FindImg():
"""
遍历Pic 目录下的图片,去路径中找图片后缀为PNG的图片,对比
"""
file_dir = (".\\BigImg")
L=[]
for root,dirs,files in os.walk(file_dir):
#print root,dirs,files #路径地址、文件夹名、文件名
for file in files:
L.append(os.path.join(root,file))
#if imgfile.endswith(".bmp"):
file_dir = (".\\result")
M=[]
for root, dirs, files in os.walk(file_dir):
for file in files:
if os.path.splitext(file)[1] == '.bmp':
#print os.path.join(root, file)
im2 = Image.open(os.path.join(root, file))
#im2.show()
M.append(os.path.join(root, file))
#print M
"""外层循环4次,内层循环多次"""
listcode = []
strtt = ""
for i in L :
print "\n",i ,"\n"
a=getImgHash(i)
PreMax = {}
Listcom = []
for j in M:
#im2 = Image.open(j)
#print j
b=getImgHash(j)
compare=getMH(a,b)
print j,u'相似度',str(compare)
PreMax.setdefault(str(compare),j)
Listcom.append(compare)
#取出最大百分比的值,再通过最大值取拿到Key,再获取图片名称
print u"\n各图片的相似度及图片路径:\n",PreMax,"\n"
print "\n所有相似度的百分值集合:\n",Listcom,"\n"
"""取出最相似的"""
a = max(Listcom)
"""拿到最相似的图片路径"""
strpath = PreMax.get(str(a),j)
"""截取字符串11-12位"""
name = strpath[11:12]
strtt += name
#name = strpath[strpath.rfind("/")+12:strpath.rfind(".")]#截取12位到.的字符串
listcode.append(name)
print "\n",listcode,"\n"
print strtt
"""下载图片"""
GetVerficode()
"""裁剪图片"""
CutCrop()
"""放大图片"""
fixed_size()
"""对比图片"""
FindImg()
没有合适的资源?快使用搜索试试~ 我知道了~
Python 建立一个模型来识别验证码成功率
共42个文件
bmp:40个
jpg:1个
py:1个
需积分: 41 22 下载量 175 浏览量
2017-07-13
22:18:53
上传
评论 1
收藏 170KB RAR 举报
温馨提示
Python 2.7版本,需要安装对应的PIL 包,不规则验证码图片无法切割准确。
资源推荐
资源详情
资源评论
收起资源包目录
pythonScript.rar (42个子文件)
pythonScript
Model
BigImg
10003.bmp 192KB
10002.bmp 192KB
10000.bmp 192KB
10001.bmp 192KB
GetCodeRuslat.py 6KB
code
1000.jpg 2KB
font
1001.bmp 1KB
1002.bmp 1KB
1003.bmp 1KB
1000.bmp 1KB
result
b
b.bmp 192KB
z
z.bmp 192KB
s
s.bmp 192KB
n
n.bmp 192KB
5
5.bmp 192KB
x
x.bmp 192KB
h
h.bmp 192KB
t
t.bmp 192KB
q
q.bmp 192KB
8
8.bmp 192KB
e
e.bmp 192KB
7
7.bmp 192KB
4
4.bmp 192KB
m
m.bmp 192KB
i
i.bmp 192KB
d
d.bmp 192KB
p
p.bmp 192KB
2
2.bmp 192KB
v
v.bmp 192KB
c
c.bmp 192KB
r
r.bmp 192KB
6
6.bmp 192KB
k
k.bmp 192KB
y
y.bmp 192KB
f
f.bmp 192KB
3
3.bmp 192KB
w
w.bmp 192KB
u
u.bmp 192KB
o
a
a.bmp 192KB
g
g.bmp 192KB
j
j.bmp 192KB
9
9.bmp 192KB
共 42 条
- 1
资源评论
〆WangBenYan゜
- 粉丝: 120
- 资源: 18
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功