NumberKeyPixel={
[(7,10),(0,12),(0,10),(0,11),(0,8),(1,14),(1,15)],
[(4,8)],
[(0,18),(0,19)],
[],
[(5,7)],
[(0,4),(0,10)],
[(2,6)],
[(2,16)],
[(0,12)],
[(2,13)]
}
from PIL import Image
import os
#存放材料图片的路径
path="C:\\vaildpic\\"
#取得材料图片
images=os.listdir(path)
存放数字的切片,0-9的图片
nubimgs=[]
#存放背景色
backpixels=[]
#存放像素对应表
pixDir={}
#首非背景色偏移值
pixBlankEndPos=[]
#这个函数用来取得这个图片中数字结构的偏移值
def GetLastBlankPosition(materialPic,x=0):
bc=materialPic.getpixel((0,0))
for i in range(15):
for j in range(24):
if materialPic.getpixel((i+x,j))!=bc:
return i
#因为只是解析没有写的很严谨,这个地方
#取得目标文件夹的图片
for image in images:
if os.path.isdir(path+image):
continue
image=Image.open(path+image)
#对于每张图片切成四份,存到字典中,取得相应的背景色,首非背景色偏移x,接下来计算用
for i in range(4):
ma=image.crop((i*15,0,(i+1)*15,24))
nubimgs.append(ma)
backpixels.append(image.getpixel((0,0)))
pixBlankEndPos.append(GetLastBlankPosition(ma))
print pixBlankEndPos
#对于每个数字图片的每个像素,如果对应位置非背景色,将该图片放到该位置的字典中,其结构如下,接下来用下面的数据统计来取得每个数字的特征像素
''' pixDir[pixel(x-x_offset,y),imgSeq]=picture<br>'''
for i in range(15):
for j in range(24):
ai=None
aj=None
pixDir[(i,j)]={}
for imgNum in range(nubimgs.__len__()):
if(nubimgs[imgNum].getpixel((i,j))!=backpixels[imgNum]):
pixDir[(i-pixBlankEndPos[imgNum],j)][imgNum]=nubimgs[imgNum]
"""nubimgs[0].putpixel ((i,j),nubimgs[imgNum].getpixel((i,j)))"""
'''下面将只有n个数字有的像素存到对应的文件夹中'''
for pix in pixDir.items():
if pix[1].__len__()<=6:
print pix
i=0
for pic in pix[1].items():
i+=1
if not os.path.exists(path+str(pix[1].__len__())):
os.mkdir(path+str(pix[1].__len__()))
pic[1].save(os.path.join(path+str(pix[1].__len__()),str(pix[0][0])+"_"+str(pix[0][1])+"__"+str(i)+".bmp"))
def getBackColors(bmp): list=[] for i in range(60): if bmp.getpixel((i,0)) not in list: list.append(bmp.getpixel((i,0))) return list
def GetLastBlankPosition(materialPic,x=0): bc=getBackColors(materialPic) for i in range(15): for j in range(24): if materialPic.getpixel((i+x,j)) not in bc: return i
def GetVaildJpgNumber(bmp): print 'GetVaildJpgNumber' vaildStr=""; backColors=getBackColors(bmp)<br> #对于一个验证码的4个数字分别验证,其x范围为n*15~(n+1)*15 for pos in range(4):<br> #取得对应位置的首绘偏移值 offset=GetLastBlankPosition(bmp,pos*15)<br> #对于0-9,分别判断对应的特征是否为背景色,如果不是解析完成,是背景色则判断下一个数字,因为3的像素基本和其他图像共享,所以如果最后没有找到特定的数字,就是3 for nr in range(0,10): isthisNr=True for pix in NumberKeyPixel[nr]: if pix[0]+offset>=15: isthisNr=False break if bmp.getpixel((pix[0]+offset+pos*15,pix[1])) in backColors : isthisNr=False break; if isthisNr and NumberKeyPixel[nr].__len__()!=0 : vaildStr+=str(nr) break if vaildStr.__len__()==pos: vaildStr+='3' print vaildStr return vaildStr
def GetVaildJpg ():
print 'GetVaildJpg'
headers={
'Accept': 'image/png, image/svg+xml, image/*;q=0.8, */*;q=0.5',
'Referer': 'http://zcc.myschool.edu.cn/',
'Accept-Language': 'zh-Hans-CN,zh-Hans;q=0.8,en-US;q=0.5,en;q=0.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko',
'Accept-Encoding': 'gzip, deflate',
'Host': 'zcc.myschool.edu.cn',
'DNT': '1',
'Connection': 'Keep-Alive',
'Cookie': sessionId
}
httpClient=httplib.HTTPConnection('zcc.myschool.edu.cn',80,timeout=300)
httpClient.request("GET",'http://zcc.myschool.edu.cn/image.jsp',None,headers)
response=httpClient.getresponse()
'''print response.getheaders()'''
stBmp=response.read()
bmp=Image.open(BytesIO(stBmp))
bmp.save('D:\PROJECT\PYTHON\catchDorm\catch.bmp')
'''bmp.show()'''
return GetVaildJpgNumber(bmp)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有