验证码自动生成
网上有很多,却都不够简单干脆,自己实现了一个。但可惜字体旋转未能成功。
# 中文四则运算
#!/usr/bin/env python
# coding=utf-8
from PIL import Image, ImageDraw, ImageFont import random
import matplotlib.pyplot as plt class ImageCaptcha(object): def __init__(self, size=(200, 60)): self.bgColor = self.random_color(200, 255) self.image = Image.new('RGB', size, self.bgColor) self.txt_num = u'一二三四五六七八九十壹貳叄肆伍陸柒捌玖拾' self.txt_op = u'加减乘除' self.txt_eq = u'等于' def random_color(self, minc=0, maxc=255): return (random.randint(minc, maxc), random.randint(minc, maxc), random.randint(minc, maxc)) def gen_text(self, cnt=6): # seq = '23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' a = random.choice(self.txt_num) b = random.choice(self.txt_op) c = random.choice(self.txt_num) d = self.txt_eq text = a+b+c+d return ''.join(text) def draw_text(self, pos, txt): draw = ImageDraw.Draw(self.image) fontSize = random.choice([38,32,36]) # font = ImageFont.truetype('Kaiti-SC-Bold.ttf', fontSize) # font = ImageFont.truetype('Arimo-Bold.ttf', fontSize) font = ImageFont.truetype('simsun.ttc', fontSize) fontColor = self.random_color(1, 150) draw.text(pos, txt, font=font, fill=fontColor) def rotate(self, angle): self.image = self.image.rotate(random.randint(-1*angle, angle), expand=0) def clear_bg(self): width, height = self.image.size for x in range(width): for y in range(height): pix = self.image.getpixel((x, y)) if pix == (0, 0, 0): self.image.putpixel((x,y), self.bgColor) def random_point(self): width, height = self.image.size x = random.randint(0, width) y = random.randint(0, height) return (x, y) def add_noise(self): start_point = self.random_point() end_point = self.random_point() draw = ImageDraw.Draw(self.image) for i in range(10): draw.line((start_point, end_point), fill=self.random_color(), width=random.randint(0,2)) def gen_captcha_image(self, text): for i, txt in enumerate(text): x = 2 + i * 35 + random.randint(-2, 2) y = random.randint(2, 10) self.draw_text((x, y), txt) self.rotate(3) self.add_noise() self.clear_bg() return self.image if __name__ == '__main__': test = ImageCaptcha() random_str = test.gen_text() print 'random_str', random_str img = test.gen_captcha_image(random_str) plt.imshow(img) print img.size
# 英文+数字
from PIL import Image, ImageDraw, ImageFont import os, random from prepare import clearNoise class ImageCaptcha(object): def __init__(self, size=(200, 60)): bgColor = self.random_color(200, 255) self.image = Image.new('RGB', size, bgColor) def random_color(self, minc, maxc): return (random.randint(minc, maxc), random.randint(minc, maxc), random.randint(minc, maxc)) def gen_text(self, cnt=6): seq = '23456789ABCDEFGHIJKLMNOPQRSTUVWXYZ' text = [random.choice(seq) for i in range(cnt)] return ''.join(text) def draw_text(self, pos, txt): draw = ImageDraw.Draw(self.image) fontSize = random.choice([38, 42, 56]) # font = ImageFont.truetype('DroidSerif-Bold.ttf', fontSize) font = ImageFont.truetype('Arimo-Bold.ttf', fontSize) fontColor = self.random_color(0, 150) draw.text(pos, txt, font=font, fill=fontColor) def rotate(self, angle): self.image = self.image.rotate(random.randint(-1*angle, angle), expand=0) def gen_captcha_image(self, text): for i, txt in enumerate(text): x = 8 + i * 30 + random.randint(-10, 10) y = random.randint(0, 10) self.draw_text((x, y), txt) # self.rotate(5) return self.image def clear_noise(path): fnames = [os.path.join(path, fname) for fname in os.listdir(path) if fname.endswith('jpg')] for fname in fnames: img = clearNoise(fname) img.save('auto/clearNoise/%s' % fname.split('/')[-1]) print img.size, fname, 'done' if __name__== '__main__': for i in range(20): test = ImageCaptcha() text = test.gen_text(6) img = test.gen_captcha_image(text) img.save('auto/origin/%s_%d.jpg' % (text, i))
每天一小步,人生一大步!Good luck~
浙公网安备 33010602011771号