Python3简单验证码识别
说好的pillow库的实战篇{:10_332:} 这次是识别简单的验证码, 先贴上代码:from PIL import Image
import requests
import re
splitter = re.compile(r'\d{30}')# 分割二值化后的图片
# distance('11110000', '00000000')
# 比较两个字符串有多少位不同, 返回不同的位数
def distance(string1, string2):
d_str1 = len(string1)
d_str2 = len(string2)
d_arr = [ * d_str2 for i in range(d_str1)]
for i in range(d_str1):
for j in range(d_str2):
if string1 == string2:
if i == 0 and j == 0:
d_arr = 0
elif i != 0 and j == 0:
d_arr = d_arr
elif i == 0 and j != 0:
d_arr = d_arr
else:
d_arr = d_arr
else:
if i == 0 and j == 0:
d_arr = 1
elif i != 0 and j == 0:
d_arr = d_arr + 1
elif i == 0 and j != 0:
d_arr = d_arr + 1
else:
d_arr = min(d_arr, d_arr, d_arr) + 1
current = max(d_arr, abs(d_str2 - d_str1))
return current
# 去除字符串里面连续的1
def no_one(string):
n_arr = splitter.findall(string)
n_arr = filter(lambda each_str: each_str != '111111111111111111111111111111', n_arr)
n_result = ''
for n_each in n_arr:
n_result += str(n_each)
return n_result
opener = requests.session()
res = opener.get('http://60.211.254.236:8402/Ajax/ValidCodeImg.ashx').content
with open('verify.gif', 'wb') as v:
v.write(res)
img = Image.open('verify.gif')
img = img.convert('L')
size = img.size
# img = img.point(table, '1')
img_arr = img.load()
# img.save('after.gif')
inc = 0
str1 = ''
str2 = ''
str3 = ''
cur_str = ''
for x in range(size):
for y in range(size):
if img_arr > 210:
cur_str += '1'
else:
cur_str += '0'
# print(img_arr, end='')
# cur_str += str(img_arr)
inc += 1
if inc == 18:
str1 = cur_str
cur_str = ''
elif inc == 36:
str2 = cur_str
cur_str = ''
elif inc == 54:
str3 = cur_str
cur_str = ''
str1 = str1[:-60]
str2 = str2[:-60]
str3 = str3[:-60]
str1 = no_one(str1)
str2 = no_one(str2)
str3 = no_one(str3)
str1 = str1.strip('1')
str2 = str2.strip('1')
str3 = str3.strip('1')
with open('./dict/plus') as plus:
with open('./dict/minus') as minus:
p = plus.read()
m = minus.read()
is_add = 1 if distance(p, str2) < distance(m, str2) else 0
arr1 = []
arr3 = []
for each in range(1, 10):
with open('./dict/{}'.format(each)) as f:
ff = f.read()
arr1.append()
arr3.append()
arr1 = sorted(arr1, key=lambda item: item)
arr3 = sorted(arr3, key=lambda item: item)
result = arr1 + arr3 if is_add else arr1 - arr3
print(result)
主要注释都写在了代码中, 涉及到的字库文件上传到了gitHub上, 我是链接 请教一下楼主怎么安装那些模块。。 leozyre 发表于 2016-6-13 22:12
请教一下楼主怎么安装那些模块。。
pip hldh214 发表于 2016-6-13 22:54
pip
感谢~ 还没学到这 不错哈,下次贴个大图 or 动图演示程序就完美了! 楼主进步好快啊 15年底也是初学者吧??? jin6220 发表于 2016-12-11 20:43
楼主进步好快啊 15年底也是初学者吧???
对, 自学半年, 目前主攻 php web 后端, Python 客户端开发 hldh214 发表于 2016-12-11 20:45
对, 自学半年, 目前主攻 php web 后端, Python 客户端开发
趁着您在线,请问我做一个论坛发帖的,如果不登陆的话,直接把浏览器里的cookies放进date里,是不是也可以啊? jin6220 发表于 2016-12-11 21:19
趁着您在线,请问我做一个论坛发帖的,如果不登陆的话,直接把浏览器里的cookies放进date里,是不是也可 ...
还有其他因素, 要看具体情况 66666{:10_249:} 66666 用captcha不香吗 厉害,可惜楼主没上号了{:10_266:} 学习学习 {:5_109:}
页:
[1]