|
| 1 | +## graphic-verification-code |
| 2 | + |
| 3 | +国人写的图形验证码库,使用 PIL ,还算好用,只有两个函数 |
| 4 | + |
| 5 | +- generate 生成图像和验证码,图像是 PIL Image 实例 |
| 6 | +- base64 生成图像和验证码,图形是 Base64 编码过的 |
| 7 | + |
| 8 | +``` |
| 9 | + def generate(self, size=(120, 30), chars=None, format='PNG', mode='RGB', bg_color=(255, 255, 255), fg_color=(0, 0, 255), font_size=18, font_file=None, length=4, draw_lines=True, line_range=(1, 2), draw_points=True, point_chance=2): |
| 10 | + |
| 11 | + """ |
| 12 | + @param size: 图片的大小,格式(宽,高),默认为(120, 30) |
| 13 | + @param chars: 允许的字符集合,格式字符串 |
| 14 | + @param format: 图片保存的格式,默认为 PNG,可选的为 GIF,JPEG,TIFF,PNG |
| 15 | + @param mode: 图片模式,默认为 RGB |
| 16 | + @param bg_color: 背景颜色,默认为白色 |
| 17 | + @param fg_color: 前景色,验证码字符颜色,默认为蓝色 #0000FF |
| 18 | + @param font_size: 验证码字体大小 |
| 19 | + @param font_file: 验证码字体,默认为 None |
| 20 | + @param length: 验证码字符个数 |
| 21 | + @param draw_lines: 是否划干扰线 |
| 22 | + @param line_range: 干扰线的条数范围,格式元组,默认为 (1, 2),只有 draw_lines 为 True 时有效 |
| 23 | + @param draw_points: 是否画干扰点 |
| 24 | + @param point_chance: 干扰点出现的概率,大小范围 [0, 100],只有 draw_points 为 True 时有效 |
| 25 | + @return: [0]: PIL Image 实例 |
| 26 | + @return: [1]: 验证码图片中的字符串 |
| 27 | + """ |
| 28 | +``` |
| 29 | + |
| 30 | +使用起来非常的简单 |
| 31 | + |
| 32 | +``` |
| 33 | +# coding=utf-8 |
| 34 | + |
| 35 | +import gvcode |
| 36 | + |
| 37 | +img, code = gvcode.generate() |
| 38 | + |
| 39 | +print code |
| 40 | + |
| 41 | +img.show() |
| 42 | +img.save('verification.jpg') |
| 43 | + |
| 44 | +``` |
| 45 | + |
| 46 | +也可以直接使用 PIL 生成验证码 |
| 47 | + |
| 48 | +``` |
| 49 | +# coding=utf-8 |
| 50 | +try: |
| 51 | + import Image, ImageDraw, ImageFont, ImageFilter |
| 52 | +except ImportError: |
| 53 | + from PIL import Image, ImageDraw, ImageFont, ImageFilter |
| 54 | +import random |
| 55 | + |
| 56 | + |
| 57 | +# 随机字母: |
| 58 | +def rndChar(): |
| 59 | + return chr(random.randint(65, 90)) |
| 60 | + |
| 61 | + |
| 62 | +# 随机颜色1: |
| 63 | +def rndColor(): |
| 64 | + return (random.randint(64, 255), random.randint(64, 255), random.randint(64, 255)) |
| 65 | + |
| 66 | + |
| 67 | +# 随机颜色2: |
| 68 | +def rndColor2(): |
| 69 | + return (random.randint(32, 127), random.randint(32, 127), random.randint(32, 127)) |
| 70 | + |
| 71 | + |
| 72 | +if __name__ == '__main__': |
| 73 | + # 240 x 60: |
| 74 | + width = 60 * 4 |
| 75 | + height = 60 |
| 76 | + image = Image.new('RGB', (width, height), (255, 255, 255)) |
| 77 | + # 创建Font对象: |
| 78 | + font = ImageFont.truetype('Arial.ttf', 36) |
| 79 | + # 创建Draw对象: |
| 80 | + draw = ImageDraw.Draw(image) |
| 81 | + # 填充每个像素: |
| 82 | + for x in range(width): |
| 83 | + for y in range(height): |
| 84 | + draw.point((x, y), fill=rndColor()) |
| 85 | + # 输出文字: |
| 86 | + for t in range(4): |
| 87 | + draw.text((60 * t + 10, 10), rndChar(), font=font, fill=rndColor2()) |
| 88 | + |
| 89 | + # 模糊: |
| 90 | + image = image.filter(ImageFilter.BLUR) |
| 91 | + image.show() |
| 92 | + image.save('code.jpg', 'jpeg') |
| 93 | + |
| 94 | +``` |
0 commit comments