zoukankan      html  css  js  c++  java
  • Django Pillow

    BBS项目数据设计

    数据库设计:
    		User
    			-id
    			-name
    			-password
    			-email
    			-phone
    			-avatar   用户头像
    			-create_date    用户注册时间
    			-blog
    		Blog
    			-id
    			-title   #博客标题
    			-site_name  #站点名称
    			-theme
    		category(分类):   #一个站点可以有多个分类,但分类只属于创建分类那个人的站点
    			-nid
    			-title
    			-blog   跟blog一对多
    			
    		tag:(文章关键字)
    			-id
    			-title
    			-blog    跟blog一对多
    			
    		article
    			-id
    			-title
    			-desc    摘要
    			-create_time    auto_add_now:当该条记录创建时,自动添加当前时间
    			-content   文章内容
    			
    			-category    一对多  一个分类可以有多个文章
    			-tag         多对多   多个文章可以有多个关键字
    			-blog        一对多   一个博客站点可以有多个文章
    			
    		commit(评论)
    			-id
    			-user     哪个用户 外键
    			-article  对哪篇文章 外键
    			-content   评论了什么内容
    			-commit_time  时间
    			
    			-parent_id  这个绑定commit 的id
    			
    			
           nid    user    article   content    parent_id
    		
    	1      1          1       111         null
    	2      2          1       222         null
    	3      3          1       333          1
    	4      4          1       444          3
    	5      3          1       555          4
    
    
    		UpandDown(点赞数 反对数)
    			-nid
    			-user     哪个用户
    			-article  对哪篇文章
    			-is_up   点赞还是点踩 
    

     Pillow

    from django.shortcuts import render, HttpResponse
    from PIL import Image,ImageDraw,ImageFont
    
    import random
    from io import BytesIO
    
    
    def get_valid_code(request):
        # 第一种方式
        with open('static/img/lhf.jpg','rb') as f:
            # 图片二进制
            data=f.read()
        return HttpResponse(data)
    
        # 第二种方式:随机生成一张图片
        # pip3 install Pillow
        # pillow 是一个图形处理的模块,功能很强强大
        # 生成一张图片,第一个参数是模式:RGB,第二个参数是图片大小,第三个参数是图片颜色
        img = Image.new('RGB', (320, 35), color=get_random_color())
        # 保存到本地
        with open('valid_code.png', 'wb') as f:
            # 直接用img的save方法,第一个参数是空文件,第二个参数图片格式
            img.save(f, 'png')
        # 打开文件,再返回
        with open('valid_code.png', 'rb') as f:
            data = f.read()
        return HttpResponse(data)
    
        #from io import BytesIO 
        #BytesIO 是在内存中生成空文件进行管理 能自动清理 速度更快
    
        # 第三种方式
        # 在内存中生成一个空文件(把它想象成 open('valid_code.png', 'wb') as f:)
        # 一个是在硬盘上,一个是在内存中
        img = Image.new('RGB', (320, 35), color=get_random_color())
        f = BytesIO()
        # 把图片保存到f中
        # 放到内存中,存取比较快,而且有自动清理
        img.save(f, 'png')
    
        data = f.getvalue()
        return HttpResponse(data)
    
    
        # 第四种方式,在图片上写文字
        img = Image.new('RGB', (320, 35), color=get_random_color())
        # 拿到画笔,把图片传入画笔
        img_draw=ImageDraw.Draw(img)
        # 生成一个字体对象,第一个参数是字体文件的路径,第二个参数是字体大小
        font=ImageFont.truetype('static/font/ss.TTF',size=25)
    
        # 第一个参数,xy的坐标,第二个参数:要写的文字,第三个参数:写文字的颜色,第四个参数:字体
        # 不同的字体是不同的ttf文件
        img_draw.text((0,0),'python',get_random_color(),font=font)
    
        f = BytesIO()
        # 把图片保存到f中
        # 放到内存中,存取比较快,而且有自动清理
        img.save(f, 'png')
    
        data = f.getvalue()
        return HttpResponse(data)    
    
  • 相关阅读:
    Test_StringBuilder
    什么是朋友
    Uncharted4-Resource-Extract
    在使用 .NET Remoting 技术开发跨进程通信时Remoting找不到请求的服务--解决方法
    SourceTree 拉取github资源包时报错:fatal: unable to access 'https://github.com/xxx.git/': OpenSSL SSL_connect: SSL_ERROR_SYSCALL in connection to github.com:443 --解决办法
    .net core3.1 文件导出报错The type initializer for 'Gdip' threw an exception 的解决方法
    .net core3.1文件下载之MimeTypeMapHelper
    .net core3.1 webapi项目部署在centos7中,通过supervisor来做进程守护遇到backoff Exited too quickly (process log may have details)的错误--并提供解决方法
    jenkins安装在ubuntu的解决方法-运维系列
    supervisord.conf配置文件详情
  • 原文地址:https://www.cnblogs.com/layerluo/p/10029177.html
Copyright © 2011-2022 走看看