Python 批量下載驗證碼圖片及切割驗證碼圖片,識別驗證碼,並以識別的文字重命令驗證碼
阿新 • • 發佈:2019-01-04
1、Python 批量下載驗證碼圖片
2、批量切割驗證碼圖片
3、識別驗證碼,並以識別的文字重命令驗證碼
#!C:/Python27 #coding=utf-8 import pytesseract from pytesser import * from PIL import Image,ImageEnhance,ImageFilter import os import fnmatch import re,time import urllib, random def GetVerficode(): #下載驗證碼圖片 for i in range(1,101): url = 'https://cas.sf-express.com/cas/imgcode?a=0.7860542547321294' print "download", i file("./code/%04d.jpg" % random.randrange(10000), "wb").write(urllib.urlopen(url).read()) def CutCrop():#分隔驗證圖片 'global data 全域性變數在整個函式中有效 ' j = 1000 for f in os.listdir(".//code//"): if f.endswith(".jpg"): print f img = Image.open('.//code//'+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座標 y = 6 img.crop((x-4, y,x+6, y+14)).save("font/%d.bmp" % j) print "j=",j j += 1 #if __name__ == '__main__': #排序讀取的檔名,按順序輸出 def ReadFileName(): global vcode l = fnmatch.filter(os.listdir('./font'), '*.bmp') for i in range(len(l)): l[i] = l[i].split('.') l[i][0] = int(l[i][0]) l.sort() for i in range(len(l)): l[i][0] = str(l[i][0]) l[i] = l[i][0] + '.' + l[i][1] #print '\n排序後:\n',l for line in l: #識別圖片 image = Image.open('./font/'+line) #二值化處理 threshold = 90 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) out = image.point(table, '1') vcode = image_to_string(out) if re.match('^[0-9a-z]+$',vcode.strip()):#如果是數字或小寫字母就列印 print vcode.strip() #out.show() ''' #修改圖片的名字為識別的名字+圖片字尾 curDir = os.getcwd() ''' def finddupl(lst): """找出 lst 中有重複的項 (與重複次數無關,且與重複位置無關) """ exists, dupl = set(), set() for item in lst: if item in exists: dupl.add(temp) else: exists.add(temp) return dupl def rename():#修改圖片的名字為識別的名字+圖片字尾 path=r'E:\pythonScript\Model\font' filelist=os.listdir(path)#該資料夾下所有的檔案(包括資料夾) global Newname,Olddir,Name Oldname = [] Newname = [] Name = {} for files in filelist:#遍歷所有檔案 Olddir=os.path.join(path,files).replace("\\","/");#原來的檔案路徑 Oldname.append(Olddir) if os.path.isdir(Olddir):#如果是資料夾則跳過 continue; filename=os.path.splitext(files)[0];#檔名 filetype=os.path.splitext(files)[1];#副檔名 #Newdir=os.path.join(path,filename[0]+filetype);#新的檔案路徑 #print u'舊的路徑:',Olddir #print u'新的路徑:',Newdir a =filename+filetype #print u"a的檔名:",a time_begin = time.clock()#記錄開始時間 image = Image.open('./font/'+a) #二值化處理 threshold = 105 #90 table = [] for i in range(256): if i < threshold: table.append(0) else: table.append(1) out = image.point(table, '1') vcode = image_to_string(out) vcode = vcode.strip() image.close() #修改檔名稱時,記得要關閉流,否則會報錯 if re.match('^[0-9a-z]+$',vcode.strip()):#如果是數字或小寫字母就列印 Newdir=os.path.join(path,vcode.strip()+filetype).replace("\\","/");#新的檔案路徑 #轉換\為/ m = os.path.join('路徑','檔名.txt') m.replace('\\','/') Newname.append(Newdir) #print u'新的路徑:',Newdir print u'識別出的驗證碼:',vcode Alert_FileName(vcode) #print "Use time: %s" % (time.clock() - time_begin)#輸出花費時間 #把新的地址放入一個集合中 #Re_name() print "最後:",Name #os.rename(Olddir,Newdir)#重新命名 for i in Name: a = Name.get(i) num = 0 for b in a: os.rename(b,path+'\\'+i+unicode(num)+".bmp") num = num +1 #重複名稱重新命名方法 def Re_name():#繼續遍歷 Newname 把相同的分為一個集合 global l; l = Newname d = {} #空字典 for a in set(l): d[a] = l.count(a) print u'統計重複個數:\n',d ''' 識別到的文字,當做Key,相同Key值的放到一個List中,有多個Key 就有多少個List ''' def Alert_FileName(vcode): alist = [Olddir] #先判斷Key 是否存在,存在返回False, 追加到集合中 dict.has_key(key) if Name.has_key(vcode): #print u"vcode的值為:",vcode,Name.has_key(vcode) Name.get(vcode).append(Olddir) #print u"Map 集合:",Name else: Name.setdefault(vcode,alist) #GetVerficode() #下載驗證碼圖片 #CutCrop() #分隔驗證碼圖片 #ReadFileName()#識別 圖片 rename()#重複名稱