=avg:#像素的值比较平均值大于记为1小于记为0 bitls=bitls+'1' else: bitls=bitls+'0' returnbitls defgetMH(i1,i2): a=aHash(i1) b=aHash(i2) dist=0; foriinrange(0,len(a)): ifa[i]==b[i]: dist=dist+1 returndist defmatch(a,rootdir): list=os.listdir(rootdir) li=[] foriinlist: re=getMH(a,rootdir+"/"+i) li.append(re) b=str(li.index(max(li))+1) a=li.index(max(li)) returnb,list[a].split(".")[0] a=match('E:/test/das4.png',"E:/test/pic4") printa

另附参考的

#-*-coding:utf-8-*-
'''
Createdon2018年5月17日
'''
fromseleniumimportwebdriver
fromPILimportImage
importrequests
importtime
importbase64
importbase64
importrequests
fromurllibimporturlencode
importjson
#requests.packages.urllib3.disable_warnings()
importdatetime
fromtimeimportstrftime
fromtimeimportsleep
fromPILimportImage
#importpytesseract
fromPILimportImage
importos
importcv2
fromnumpyimportaverage,dot,linalg
importheapq
importcollections
fromlib.readConfigimportReadconfig
conf=Readconfig()
filedir=conf.getConfigValue("filedir")
defgetGray(image_file):
tmpls=[]
forhinrange(0,image_file.size[1]):#h
forwinrange(0,image_file.size[0]):#w
tmpls.append(image_file.getpixel((w,h)))
returntmpls
defgetAvg(ls):#获取平均灰度值
returnsum(ls)/len(ls)
defgetMH(i1,i2):
a=getImgHash(i1)
b=getImgHash(i2)
dist=0;
foriinrange(0,len(a)):
ifa[i]==b[i]:
dist=dist+1
returndist
defgetImgHash(fne):
image_file=Image.open(fne)#打开
image_file=image_file.resize((35,35))#重置图片大小我12pxX12px
image_file=image_file.convert("L")#转256灰度图
Grayls=getGray(image_file)#灰度集合
avg=getAvg(Grayls)#灰度平均值
bitls=''#接收获取0或1
forhinrange(1,image_file.size[1]-1):#h
forwinrange(1,image_file.size[0]-1):#w
ifimage_file.getpixel((w,h))>=avg:#像素的值比较平均值大于记为1小于记为0
bitls=bitls+'1'
else:
bitls=bitls+'0'
returnbitls
defmatch1(a,rootdir):
list=os.listdir(rootdir)
li=[]
foriinlist:
#printrootdir+"/"+i
re=getMH(a,rootdir+"/"+i)
li.append(re)
#printli
#printmax(li)
b=str(li.index(max(li))+1)
returnb
defg_code(pic):
dic={"1":"2","2":"3","3":"4","4":"5","5":"6","6":"7","7":"8","8":"9",
"9":"a","10":"b","11":"c","12":"d","13":"e","14":"f","15":"g","16":"h",
"17":"i","18":"j","19":"k","20":"m","21":"n","22":"p","23":"q","24":"r",
"25":"s","26":"t","27":"u","28":"v","29":"w","30":"x","31":"y","32":"z"}
img=Image.open(pic)
a=img.size[0]
b=img.size[1]
p1=filedir+r'eos_tdym/lib/pic/das1.png'
p2=filedir+r'eos_tdym/lib/pic/das2.png'
p3=filedir+r'eos_tdym/lib/pic/das3.png'
p4=filedir+r'eos_tdym/lib/pic/das4.png'
dir1=filedir+r'eos_tdym/lib/pic/pic1'
dir2=filedir+r'eos_tdym/lib/pic/pic2'
dir3=filedir+r'eos_tdym/lib/pic/pic3'
dir4=filedir+r'eos_tdym/lib/pic/pic4'
cropped=img.crop((0,0,25,30))#(left,upper,right,lower)
cropped.save(p1)
cropped=img.crop((25,0,50,30))#(left,upper,right,lower)
cropped.save(p2)
cropped=img.crop((50,0,75,30))#(left,upper,right,lower)
cropped.save(p3)
cropped=img.crop((75,0,96,30))#(left,upper,right,lower)
cropped.save(p4)
re1=str(match1(p1,dir1))
re2=str(match1(p2,dir2))
re3=str(match1(p3,dir3))
re4=str(match1(p4,dir4))
printu"获取到验证码:"+dic[re1]+dic[re2]+dic[re3]+dic[re4]
returndic[re1],dic[re2],dic[re3],dic[re4]
defg_code1(pic):
dic={"1":"2","2":"3","3":"4","4":"5","5":"6","6":"7","7":"8","8":"9",
"9":"a","10":"b","11":"c","12":"d","13":"e","14":"f","15":"g","16":"h",
"17":"i","18":"j","19":"k","20":"m","21":"n","22":"p","23":"q","24":"r",
"25":"s","26":"t","27":"u","28":"v","29":"w","30":"x","31":"y","32":"z"}
img=Image.open(pic)
a=img.size[0]
b=img.size[1]
p1="pic5/das1.png"
p2="pic5/das2.png"
p3="pic5/das3.png"
p4="pic5/das4.png"
dir1="pic1"
dir2="pic2"
dir3="pic3"
dir4="pic4"
cropped=img.crop((0,0,25,30))#(left,upper,right,lower)
cropped.save(p1)
cropped=img.crop((25,0,50,30))#(left,upper,right,lower)
cropped.save(p2)
cropped=img.crop((50,0,75,30))#(left,upper,right,lower)
cropped.save(p3)
cropped=img.crop((75,0,96,30))#(left,upper,right,lower)
cropped.save(p4)
re1=match1(p1,dir1)
re2=match1(p2,dir2)
re3=match1(p3,dir3)
re4=match1(p4,dir4)
printdic[re1]
printdic[re2]
printdic[re3]
printdic[re4]
returndic[re1],dic[re2],dic[re3],dic[re4]

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python图片操作技巧总结》、《Python数据结构与算法教程》、《PythonSocket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

热门推荐

1 毛坯房验收经验和常识 看了之后再验房心里有底
2 二手房收房如何交接 二手房收房注意问题
3 专业验收毛坯房的价格 商品房验收合格的标准
4 精装房怎么验收 精装房请验房师有用吗
5 一般要到哪里找验房师 验房师有哪些作用呢
6 请人验房一般是多少钱 验房师费用是多少
7 怎样测量房子面积 建筑面积和使用面积怎么算
8 收房需要注意什么 仔细检查不松懈
9 收房时三书一证一表是什么 主要作用介绍
10 交房时交房税费有哪些 本文为你一一讲解
11 验房都需要验什么 要做哪些准备呢
12 毛坯房验房师有必要请吗 毛坯房装修完如何验收
13 地下室防水工程质量验收规范详解
14 水性涂料、油性涂料区别介绍
15 零基础布艺DIY工坊 教你做超萌猫头鹰钥匙包
16 三棵树漆怎么样?三棵树漆官方网站
17 家庭“装修套餐”中猫腻你知道吗?
18 小空间大浴望 卫浴间装修巧支招