zoukankan      html  css  js  c++  java
  • 验证码

    1、安装pillow

    2、html中引入图片填写路径

    <img class="valid_img" src="/get_valid_img/" alt="" width="200" height="40">
    def get_valid_img(request):
    
        # 第一步
        # with open("lufei.jpg","rb") as f :
        #     data=f.read()
    
    
        # 第二步:
        # import PIL
        # from PIL import Image
        # img = Image.new(mode='RGB', size=(120, 30), color=(123, 222, 255))
        # f=open('a.png','wb')
        # img.save(f,"png")
        #
        # f=open("a.png","rb")
        # data=f.read()
    
        # 第三步:
    
        # from io import BytesIO
        # f=BytesIO()
        #
        # from PIL import Image
        # img = Image.new(mode='RGB', size=(120, 30), color=(0, 255, 0))
        # img.save(f,"png")
        # data=f.getvalue()
        # return HttpResponse(data)
    
        # 第四步:
    
        from io import BytesIO
        f=BytesIO()
        from PIL import Image,ImageDraw,ImageFont
        #
        # # 画字
        # img = Image.new(mode='RGB', size=(120, 30), color=(0, 255, 0))
        # draw = ImageDraw.Draw(img, mode='RGB')
        # font=ImageFont.truetype("blog/static/dist/fonts/kumo.ttf",28)
        # draw.text([15,2],'y',"red",font=font)
        #
        # img.save(f,"png")
        # data=f.getvalue()
    
        # 第五步:
        import random
        img = Image.new(mode='RGB', size=(120, 30),
                        color=(random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)))
        draw = ImageDraw.Draw(img, mode='RGB')
    
        char_list=[]
        # 画字
        for i in range(5):
            char = random.choice([chr(random.randint(65, 90)), str(random.randint(1, 9)),chr(random.randint(97, 122)),])
    
            font = ImageFont.truetype("app/static/dist/fonts/kumo.ttf", 28)
            draw.text([i * 24, 0], char, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
    
    
                      font=font)
    
            char_list.append(char)
    
        #=========================
        width = 120
        height = 30
        def rndColor():
            """
            生成随机颜色
            :return:
            """
            return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
        # 写干扰点
        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.save(f,"png")
        data=f.getvalue()
    
        s=''.join(char_list)
    
        request.session["valid_code"]=s
    
        '''
        Dajngo:
        set_cookie("sessionId","asdsa234asd3sad")
        in session表
        sessionkey           sessiondata
        asdsa234asd3sad      {"valid_code":"qqqqq"}
        '''
        return HttpResponse(data)

    点击 刷新验证码

    <script>
        $(".valid_img").click(function(){
            $(this)[0].src+="?"
        })
    </script>
  • 相关阅读:
    Android 视图切换库的使用
    Ext3.4--TreeGridDemo
    Extjs不错的博客
    Extjs学习笔记--(六,选择器)
    Webservice简单案例
    Extjs学习笔记--(五,事件)
    Extjs学习笔记--(四,基本函数介绍)
    Extjs学习笔记--(三,调试技巧)
    SQL集合运算:差集、交集、并集
    Extjs学习笔记--(二)
  • 原文地址:https://www.cnblogs.com/kunixiwa/p/8001407.html
Copyright © 2011-2022 走看看