验证码函数

import random
def Verification_code(n):
    res=[[chr(i) for i in range(ord('0'),ord('9')+1)],
       [chr(i) for i in range(ord('a'),ord('z'))],
       [chr(i) for i in range(ord('A'),ord('Z'))]]
    yanzhengma=''
    for i in range(n):
        choice1=random.choice(res)
        choice2=random.choice(choice1)
        yanzhengma+=choice2
    print(yanzhengma)
    return yanzhengma
res=Verification_code(4)

#检验验证码是否正确
def Verification_input(n):
    while True:
        re = Verification_code(n)
        code = input('请输入括号里的验证码,不区分大小写【{0}】:'.format(re))
        if code.strip().lower() != re.lower():
            print('您输入的验证码有误,请注意区分数字0和字母o 数字1和字母l')
        else:
            return True
View Code

 

Django 图片验证码配置文件

创建utils目录,接着创建random_check_code.py,在里边写函数rd_check_code

import random
from PIL import Image,ImageFont,ImageDraw,ImageFilter

def rd_check_code(width=100, height=35, char_length=4, font_file='kumo.ttf', font_size=35):
    # width:图片宽度 height:图片高度 char_length:验证码个数 font_file:验证码字体文件路径 font_size:验证码字符大小
    code = []
    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode='RGB')

    def rndChar():
        """
        生成随机字母   
        :return:
        """
        res = [[chr(i) for i in range(ord('0'), ord('9') + 1)],  # 生成0到9
               [chr(i) for i in range(ord('a'), ord('z'))],      # 生成a到z
               [chr(i) for i in range(ord('A'), ord('Z'))]]      # 生成A到Z
        choice1=random.choice(res)
        choice2=random.choice(choice1)
        return choice2

    def rndColor():
        """
        生成随机颜色
        :return:
        """
        return (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    # 写文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        char = rndChar()
        code.append(char)
        h = random.randint(0, 4)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())

    # 写干扰点
    for i in range(10):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

    # 写干扰圆圈
    for i in range(10):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())

    # 画干扰线
    for i in range(3):
        x1 = random.randint(0, width)
        y1 = random.randint(0, height)
        x2 = random.randint(0, width)
        y2 = random.randint(0, height)

        draw.line((x1, y1, x2, y2), fill=rndColor())

    img = img.filter(ImageFilter.DETAIL)  # 滤镜
    return img, ''.join(code)
random_check_code.py

 

在视图函数中调用

from django.shortcuts import render,redirect,HttpResponse
def check_code(request):
    from io import BytesIO
    from utils.random_check_code import rd_check_code

    img,code = rd_check_code()
    stream = BytesIO()
    img.save(stream, 'png')
    data = stream.getvalue()
    request.session['code'] = code
    return HttpResponse(data)
View Code

 

posted @ 2017-09-14 21:32  _慕  阅读(1583)  评论(0编辑  收藏  举报
Title
返回顶部