简易验证码图片内容破解
最近新招的实习生妹子,弄的一个 验证码破解脚本, 据说准确率80% 。 自己已经很少写代码了,就直接贴上实习生MM的脚本
# -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ import os,shutil import re #import sys os.chdir(r'D:\Users\katharine.li\Desktop\资料\其他\验证码\img') from pytesser3 import image_to_string from PIL import Image, ImageFilter, ImageEnhance import matplotlib.pyplot as plt #import requests def get_code(image,num): img = Image.open(image) # img = img.resize((256,256)) ##标准化图片大小 ###修改RGB三个通道参数去除干扰线并转为黑白 pixdata = img.load() for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][0] > num: pixdata[x, y] = (0, 0, 0) for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][1] < 50: pixdata[x, y] = (255, 255, 255) for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][2] < num: pixdata[x, y] = (0, 0, 0) # else: pixdata[x, y] = (255, 255, 255) ###通过高斯模糊、边缘增强、平滑等去除干扰点 img = img.filter(ImageFilter.GaussianBlur(radius = 1)) img = img.filter(ImageFilter.EDGE_ENHANCE) # img = img.filter(ImageFilter.GaussianBlur(radius = 1)) # img = img.filter(ImageFilter.EDGE_ENHANCE) img = img.filter(ImageFilter.SMOOTH) # plt.imshow(img) ##若此时的图片文字之间还有线相连,或者干扰点较多,则无法识别,可以修改上面的参数,或者刷新验证码重新识别 vcode = image_to_string(img) vcode = re.sub("\W", "", vcode) if len(vcode) == 5: return vcode else: img = Image.open(image) pixdata = img.load() for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][1] > num: pixdata[x, y] = (0, 0, 0) for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][2] < 50: pixdata[x, y] = (255, 255, 255) for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][0] < num: pixdata[x, y] = (0, 0, 0) img = img.filter(ImageFilter.GaussianBlur(radius = 1)) img = img.filter(ImageFilter.EDGE_ENHANCE) img = img.filter(ImageFilter.SMOOTH) vcode = image_to_string(img) vcode = re.sub("\W", "", vcode) if len(vcode) == 5: return vcode else: img = Image.open(image) pixdata = img.load() for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][2] > num: pixdata[x, y] = (0, 0, 0) for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][0] < 50: pixdata[x, y] = (255, 255, 255) for y in range(img.size[1]): for x in range(img.size[0]): if pixdata[x, y][1] < num: pixdata[x, y] = (0, 0, 0) img = img.filter(ImageFilter.GaussianBlur(radius = 1)) img = img.filter(ImageFilter.EDGE_ENHANCE) img = img.filter(ImageFilter.SMOOTH) vcode = image_to_string(img) vcode = re.sub("\W", "", vcode) if len(vcode) == 5: return vcode rootdir = r'D:\Users\katharine.li\Desktop\资料\其他\验证码\img' list = os.listdir(rootdir) #列出文件夹下所有的目录与文件 for i in range(0,len(list)): #for i in range(1,20): path = os.path.join(rootdir,list[i]) if os.path.isfile(path): try: vcode = get_code(path,220) if len(vcode) == 5: print(list[i],':',vcode) shutil.move(list[i],'../decode/'+ vcode + '.jpg') except: pass continue