一. 验证码生成
方法1. 利用PIL库的ImageDraw实现绘图,此法参考博客实现:
#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Created on Tue Mar 27 15:45:04 2018 @author: lps """ from PIL import Image, ImageDraw, ImageFont, ImageFilter import random import cv2 import numpy as np import matplotlib.pyplot as plt path = \'/media/lps/python-3.5.2.amd64/Lib/site-packages/matplotlib/mpl-data/fonts/ttf/\' # 选择字体 data_path = \'/home/lps/yanzm/\' # random chr def rndChar(): return chr(random.randint(65, 90)) # 随机字母 def rndInt(): return str(random.randint(0,9)) # 随机数字 def rndColor(): return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) # 随机颜色 def rndColor2(): return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) # 随机颜色 def gaussian_noise(): # 高斯噪声 mu = 125 sigma = 20 return tuple((np.random.normal(mu, sigma, 3).astype(int))) def rotate(x, angle): # 旋转 M_rotate = cv2.getRotationMatrix2D((x.shape[0]/2, x.shape[1]/2), angle, 1) x = cv2.warpAffine(x, M_rotate, (x.shape[0], x.shape[1])) return x width = 180 * 4 height = 180 def gen_image(num): for l in range(num): image = Image.new(\'RGB\', (width, height), (255, 255, 255)) # 先生成一张大图 font = ImageFont.truetype(path+\'cmb10.ttf\', 36) draw = ImageDraw.Draw(image) # 新的画板 for x in range(0,width): for y in range(0,height): draw.point((x, y), fill=rndColor()) label = [] for t in range(4): # 每一张验证码4个数字 numb = rndInt() draw.text((180 * t + 60+10, 60+10), numb, font=font, fill=rndColor2()) label.append(numb) with open(data_path+"label.txt","a") as f: for s in label: f.write(s + \' \') f.writelines("\n") # 写入label img = image.filter(ImageFilter.GaussianBlur(radius=0.5)) img = np.array(img) img1 = np.array([]) for i in range(0,4): img0 = img[:, 180*i: 180*i+180] # 提取含有验证码的小图 angle = random.randint(-45, 45) img0 = rotate(img0, angle) # 对小图随机旋转 if img1.any(): img1 = np.concatenate((img1, img0[60:120, 60:120, :]), axis=1) else: img1 = img0[60:120, 60:120, :] plt.imsave(data_path+\'src/\' + str(l)+\'.jpg\', img1) # 保存结果 if __name__==\'__main__\': gen_image(100)