• 博客访问： 126093
• 博文数量： 16
• 博客积分： 2000
• 博客等级： 大尉
• 技术积分： 195
• 用 户 组： 普通用户
• 注册时间： 2007-07-29 08:28

2015年（1）

2010年（15）

2010-04-22 09:46:17

 ```#coding:utf-8 import urllib2 from PIL import Image from PIL import ImageFilter import os,sys def getImage():         '''网上获取图片'''     global time,pixels     url = 'http://jiaowu.dlpu.edu.cn/ACTIONVALIDATERANDOMPICTURE.APPPROCESS'     fname = 'validatecode.bmp'     open(fname,'wb').write(urllib2.urlopen(url).read())     img = Image.open(fname).convert('L')     return img      def getMatrix(i):         '''把模板文件读入数组'''     global cache,time     if i in cache.keys():         return cache[i]     arr = [[0 for col in range(6)] for row in range(10)]     f = open('%d.txt'%i)     for y in range(10):         l = f.readline()         for x in range(6):             arr[y][x] = int(l[x])     cache[i] = arr     return arr def recognize(img,i):         '''返回img和数字i的识别率rate'''     rate = 0     arr = img.load()         for x in range(6):         for y in range(10):             if arr[x,y]<128 and getMatrix(i)[y][x]==0:                 rate += 1             elif arr[x,y]>128 and getMatrix(i)[y][x]==1:                 rate += 1     return rate os.chdir('./') cache = {} img = getImage() left = 7 up = 5 right = 13 down = 15 for i in range(4):     #把图片切割成仅容下数字的四个图片     im = img.crop((left,up,right,down))     #下面8行是识别过程     rec_n = -1     rec_rate = -1     for n in range(10):         t = recognize(im,n)         if t > rec_rate:             rec_n = n             rec_rate = t     sys.stdout.write('%d'%rec_n)     #下面注释行是学习过程     #im.save('%d.bmp'%i)     #txt = open('%d.try.txt'%i,'w')     #arr = im.load()     #for y in range(10):         #for x in range(6):             #if arr[x,y] > 128:                 #txt.write('1')                 #else:                 #txt.write('0')         #txt.write(os.linesep)     #txt.close()     left += 13     right += 13 print ' ' raw_input('Press enter key to continue...') ```