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

    四种返回前端的方法

    现在介绍第四种

    import json
    import random
    from io import BytesIO
    from  PIL import Image, ImageDraw, ImageFont
    
    def get_rgb_func():
        """
        获取三原色
        :return: 三个元组
        """
        return (random.randint(1,255),random.randint(1,255),random.randint(1,255))
    
    
    def get_imge(request):
        image = Image.new(mode="RGB", size=(130, 60), color=get_rgb_func())
        draw = ImageDraw.Draw(image)
        font = ImageFont.truetype("static/fonts/kumo.ttf",size=36)
        f = BytesIO()
        temp = []
        for i in range(5):
            random_char = random.choice([str(random.randint(0,9)),chr(random.randint(65,90)),chr(random.randint(97,122))])
            draw.text((i*27,10),random_char,get_rgb_func(),font=font)
            temp.append(random_char)
        width = 120
        height = 80
    
        for i in range(80):
            draw.point((random.randint(0,width),random.randint(0,height)),fill=get_rgb_func())
    
        for i in range(10):
            x1=random.randint(0,width)
            x2=random.randint(0,width)
            y1=random.randint(0,height)
            y2=random.randint(0,height)
            draw.line((x1,y1,x2,y2),fill=get_rgb_func())
        for i in range(40):
            draw.point([random.randint(0, width), random.randint(0, height)], fill=get_rgb_func())
            x = random.randint(0, width)
            y = random.randint(0, height)
            draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_rgb_func())
        image.save(f, "png")
        data = f.getvalue()
        request.session["random_char"] = ''.join(temp)
        return HttpResponse(data)
  • 相关阅读:
    改进RazorPad
    ViewBag、ViewData和TempData的使用和区别
    MVC3路由设置访问后缀 html jsp
    RazorPad中的ModelProvider
    使用NUnit进行项目的单元测试
    有关WCF的契约问题
    常用的Windows批处理
    SmartBusinessDevFramework架构设计-2:结构图示
    SQL中查询语句的使用
    林子祥
  • 原文地址:https://www.cnblogs.com/huyangblog/p/8419295.html
Copyright © 2011-2022 走看看