zoukankan      html  css  js  c++  java
  • python生成随机验证码

    在web登录中使用验证码,本质是在前端的img标签的src属性,动态传一张验证码图片。而动态使用验证码功能,则需使用pillow模块来动态生成验证码。

    验证码本质

    # 前端
    <img src="/check_code/" style=" 120px; height: 30px;">
    
    # url
    url(r'^check_code/', views.check_code),
    
    # 后端
    def check_code(request):
        f = open("static/1.jpg","rb")
        data = f.read()
        f.close()
        return HttpResponse(data)
    

    pillow模块使用

    def check_code(request):
        from PIL import Image,ImageDraw,ImageFont
        # 创建一张图片对象
        img = Image.new(mode="RGB", size=(120, 30), color=(0,255,0))
    
        # 创建一个画笔对象
        draw = ImageDraw.Draw(img, mode="RGB")
    
        # 生成随机字符串
        check_str = "".join([chr(random.randint(65,90)) for i in range(5)])
    
        # 写入随机字符串
        draw.text([0,0], check_str, "black",)
    
        # 存储到文件
        f = open("2.png", "wb")
        img.save(f, "png")
        f.close()
        # 返回到前端
        return HttpResponse(f1.read)
    
        # 存储到内存
        from io import BytesIO
        f1 = BytesIO()
        img.save(f1, "png")
        # 返回到前端
        return HttpResponse(f1.getvalue())
    

    pillow生成验证码

    封装图片验证码模块

    import random
    from PIL import ImageDraw,Image,ImageFont,ImageFilter
    def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
        """
        
        :param  生成图片宽度(int)
        :param height:生成图片高度(int)
        :param char_length: 验证码长度(int)
        :param font_file:  字体文件(file_path)
        :param font_size: 字体大小(int)
        :return: (img,str)图片对象 和 验证码字符串
        """
        code = []
        img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
        draw = ImageDraw.Draw(img, mode='RGB')
    
        def rndChar():
            """
            生成随机字母
            :return:
            """
            return chr(random.randint(65, 90))
    
        def rndColor():
            """
            生成随机颜色
            :return:
            """
            return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 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(40):
            draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
    
        # 写干扰圆圈
        for i in range(40):
            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(5):
            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.EDGE_ENHANCE_MORE)
        return img, ''.join(code)
    

    图片验证码示例

    def check_code(request):
        # 内存读写模块
        from io import BytesIO
        # 自定义验证码模块
        from utils import random_check_code
        img,check_str = random_check_code.check_code(120,30,5,"utils/domi.ttf",28)
        stream = BytesIO()
        img.save(stream,"png")
        request.session["check_str"]=check_str
        return HttpResponse(stream.getvalue())
    

    参考博客

  • 相关阅读:
    大端序与小端序
    中断分类
    PHP开发框架[国内框架]
    PHP开发框架[流行度排名]
    ecshop 后台分页功能
    Windows下phpStudy中的Apache无法启动的排查方法
    Windows里配置Apache2.2+PHP5.3+mod_fcgid运行高效的FastCGI模式
    Apache多虚拟主机多版本PHP(5.2+5.3+5.4)共存运行配置全过程
    让 Node.js 支持 ES6 的语法
    微信小程序请求wx.request数据,渲染到页面
  • 原文地址:https://www.cnblogs.com/zouruncheng/p/7150673.html
Copyright © 2011-2022 走看看