Pillow模块——生成随机验证码
urls.py
path('get_code/',views.get_code),
views.py中
from PIL import Image,ImageFont,ImageDraw
"""
Image 产生图片
ImageFont 字体样式
ImageDraw 画笔对象
"""
from io import BytesIO,StringIO
"""
BytesIO 在内存中临时存储 读取的时候以bytes格式为准
StringIO 在内存中临时存储 读取的时候以字符串格式为准
"""
import random
def get_random()
return random.randint(0,255),random.randint(0,255),random.randint(0,255)
def get_code_func(request)
# 1.推导步骤1:直接读取图片文件返回
# with open(r'F:\BBS\static\img\123.png','rb') as f:
# data = f.read()
# return HttpResponse(data)
# 2.推导步骤2:随机产生图片动态返回 第三方pillow模块
# img_obj = Image.new('RGB',(350,35),'green')
# with open(r'xxx.png','wb') as f:
# img_obj.save(f,'png')
# with open(r'xxx.png','rb') as f:
# data = f.read()
# return HttpResponse(data)
# 3.推导步骤3:针对图片的保存与读取做优化 内存管理器
# img_obj = Image.new('RGB',(350,35),'yellow')
# io_obj = BytesIO()
# img_obj.save(io_obj,'png')
# return HttpResponse(io_obj.getvalue())
# 4.推导步骤4:图片颜色是可以随机变换的
# img_obj = Image.new('RGB',(350,35),get_random())
# io_obj = BytesIO()
# img_obj.save(io_obj,'png')
# return HttpResponse(io_obj.getvalue())
# 5.推导步骤5:编写验证码
# 先产生图片对象
img_obj = Image.new('RGB',(350,35),get_random())
# 将图片对象交给画笔对象
draw_obj = ImageDraw.Draw(img_obj)
# 确定字体样式(ttf文件)
font_obj = ImageFont.truetype('static/font/123.ttf',35)
# 产生随机验证码
code = ''
for i in range(5):
random_upper = chr(random.randint(65,90))
random_lower = chr(random.randint(97,122))
random_int = str(random.randint(0,9))
# 三选一
temp_choice = random.choice([random_upper,random_lower,random_int])
# 写到图片上
draw_obj.text((i*60 + 45 ,0),temp_choice,font=font_obj)
code += temp_choice
# 后端保存验证码 便于后续的比对
request.session['code'] = code
io_obj = BytesIO()
img_obj.save(io_obj,'png')
return HttpResponse(io_obj.getvalue())
html页面中
<img src="/get_code/" alt="" style="根据实际情况设置" id="code">
// 验证码动态刷新
$('#code').click(function () {
let oldSrc = $(this).attr('src');
$(this).attr('src', oldSrc + '?')
})
Pillow模块——生成随机验证码的更多相关文章
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- pillow实例 | 生成随机验证码
1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...
- 基于Python Pillow库生成随机验证码
from PIL import Image from PIL import ImageDraw from PIL import ImageFont import random class ValidC ...
- Django中生成随机验证码(pillow模块的使用)
Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...
- Python 生成随机验证码
Python生成随机验证码 Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...
- Python生成随机验证码
Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
- struts2生成随机验证码图片
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- 使用模块PIL 生成 随机验证码
--------------默认自己无能,无疑是给失败制造机会!你认为自己是什么样的人,就将成为什么样的人. 要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创 ...
随机推荐
- 安装opencv相关
1.查看当前OpenCV的版本 pkg-config --modversion opencv 2.查找有关OpenCV文件 find / -iname "*opencv*" //- ...
- File encoding has not been set, using platform encoding UTF-8, i.e. build is platform dependent
IDEA编译报以下错误 File encoding has not been set, using platform encoding UTF-8, i.e. build is platform de ...
- python和java语法对比
python java 不同的关键字 except,nolocal,as,assert,async,pass ,await,from,raise,global,in,del,with,lambda ...
- pyecharts 学习使用网址
pyecharts新版官方手册地址:https://pyecharts.org/#/zh-cn/intro 或http://pyecharts.org/#/?id=pyecharts或http://p ...
- HTML-CSS常用代码
注释标签:对代码进行说明<!-- 单行注释,也可以对多行文字进行注释 -->常用格式标签<b>加粗</b> <i>斜体</i> <u& ...
- Charles抓包数据导入postman
Charles抓包数据导入postman 1.先抓取到对应的请求,右键,再点击Copy cURL Request,操作如下图所示: ...
- IPAD做电脑的绘图板
方法:Microsoft远程桌面 即 RD CLIENT
- 【读书笔记】排列研究-置换角度(分解为Products Of Cycles) 含GroupExploer使用
upd 2020-08-06 23:11完成了最初稿 目录 定义 开胃菜 entrée 群论角度 应用:几何变换 当然要从第一类斯特林数的角度来考虑一下 一个排列的type定义 排旗公式 应用-共轭排 ...
- BiliBili常用API
BiliBili 爬虫b站视频信息 api 视频简要信息 http://api.bilibili.com/x/web-interface/archive/stat?aid=170001 http:// ...
- MySQL长时间不登录密码过期解决办法
MySql管理工具 Navicat for MySql的时候,弹出了一个1862 - MySql Your password has expired.To log in you must change ...