• 生成图片验证码


    # 导入类视图
    from django.http import HttpResponse
    # 导入图片库
    # 绘画库
    from PIL import ImageDraw
    #定义验证码
    # 图片库
    from PIL import Image
    # 随机库
    import random
    # 文件流
    import io
    import redis
    
    #定义ip端口
    host = "localhost"
    port = 6379
    #建立连接
    r=redis.Redis(host=host,port=port)
    
    class MyCode(View):
    
        #定义随机验证颜色
        def get_random_color(self):
            R = random.randrange(255)
            G = random.randrange(255)
            B = random.randrange(255)
    
            return (R,G,B)
    
        #随机验证码
        def get(self,request):
            #画布
            img_size = (110,50)
            #定义颜色类型
            image = Image.new("RGB",img_size,'white')
            #画笔
            draw = ImageDraw.Draw(image,'RGB')
            #定义随机字符串
            source = '0123456789'
            #容器,用来接收随机字符串
            code_str = ''
            for i in range(4):
                #获取字体颜色
                text_color = self.get_random_color()
                #获取字符串
                tmp_num = random.randrange(len(source))
                #获取字符集
                random_str = source[tmp_num]
                #添加到容器中
                code_str += random_str
                #绘制
                #横坐标10+30*i
                #纵坐标20
                draw.text((10+30*i,20),random_str,text_color)
            #文件流缓冲区
            buf = io.BytesIO()
            #将图片保存到缓冲区
            image.save(buf,'png')
            #将随机码存储到redis中
            r.set('code',code_str)
    
            return HttpResponse(buf.getvalue(),'image/png')
    
    
  • 相关阅读:
    VC 编译 MATLAB 的 mex 文件
    MATLAB 与 Excel 接口
    MATLAB 编译器的使用
    为什么安装了MinGW之后,还是不能在Matlab中使用mex?
    matlab文件操作
    matlab外部程序接口-excel
    数字图像加密-同态加密方案
    matlab数字图像简单的加密方法
    matlab中矩阵的表示与简单操作
    linux 安装eccodes环境
  • 原文地址:https://www.cnblogs.com/bronyaa/p/12767487.html
Copyright © 2020-2023  润新知