django-生成随机验证码
pip3 install pillow
pip3 install pillow
基本使用
from PIL import Image #导入模块
img=Image.new(mode="RGB",size=(120,40),color="yellow")
f=open("validCode.png","wb")
img.save(f,"png")
with open("validCode.png","rb") as f:
data=f.read()
return HttpResponse(data)
from PIL import Image #导入模块
img=Image.new(mode="RGB",size=(120,40),color="yellow")
f=open("validCode.png","wb")
img.save(f,"png")
with open("validCode.png","rb") as f:
data=f.read()
return HttpResponse(data)
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
img=Image.new(mode="RGB",size=(120,40),color="yellow")
draw=ImageDraw.Draw(img,mode='RGB')
draw.point([100,100],fill=255,255,255)
#第一个参数:表示坐标
#第二个参数:表示颜色
img=Image.new(mode="RGB",size=(120,40),color="yellow")
draw=ImageDraw.Draw(img,mode='RGB')
draw.point([100,100],fill=255,255,255)
#第一个参数:表示坐标
#第二个参数:表示颜色
draw.line((100,100,300,100), fill=(255, 255, 255))
#第一个表示起始坐标和结束坐标
#第二个参数:表示颜色
draw.line((100,100,300,100), fill=(255, 255, 255))
#第一个表示起始坐标和结束坐标
#第二个参数:表示颜色
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
draw.arc((100,100,300,300),0,90,fill="red")
# 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
# 第二个参数:表示开始角度
# 第三个参数:表示结束角度
# 第四个参数:表示颜色
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
draw.arc((100,100,300,300),0,90,fill="red")
# 第一个参数:表示起始坐标和结束坐标(圆要画在其中间)
# 第二个参数:表示开始角度
# 第三个参数:表示结束角度
# 第四个参数:表示颜色
draw.text([0,0],'python',"red")
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
draw.text([0,0],'python',"red")
# 第一个参数:表示起始坐标
# 第二个参数:表示写入内容
# 第三个参数:表示颜色
验证码的几种使用方式
#方式一
#通过静态文件的目录来定位路径
import os
path = os.path.join(settings.BASE_DIR,"static","img","ss.jpg") #BASE_DIR表示settings的上一级目录的上一级目录
with open(path,"rb") as f:
data=f.read()
#方式二
from PIL import Image #导入PIL模块
img=Image.new(mode="RGB",size=(120,40),color="yellow") #创建画笔
f=open("validCode.png","wb") #保存在本地
img.save(f,"png")
with open("validCode.png","rb") as f: #进行读取
data=f.read()
return HttpResponse(data) #然后返回给前端
'''
缺点:
会在服务端的根目录下产生一个文件,我们不应该让它产生文件
'''
#方式三
'''
把文件读到内存中去
'''
from io import BytesIO
from PIL import Image
img=Image.new(mode="RGB",size=(120,40),color="blue")
f=BytesIO()
img.save(f,"png")
data=f.getvalue()
return HttpResponse(data)
#方式四
'''
随机产生验证码
'''
# def text(self, xy, text, fill=None, font=None, anchor=None,*args, **kwargs):
from io import BytesIO #把内容保存到内存中
import random
from PIL import Image,ImageDraw,ImageFont #导入图画,画笔,字体
img = Image.new(mode="RGB", size=(120, 40), color=(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
draw=ImageDraw.Draw(img,"RGB")
font=ImageFont.truetype("blog/static/font/kumo.ttf",25)
def fandomColor():
'''
生成随机颜色
:return:
'''
random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
valid_list=[]
for i in range(5):
random_num=str(random.randint(0,9))
random_lower_zimu=chr(random.randint(65,90))
random_upper_zimu=chr(random.randint(97,122))
random_char=random.choice([random_num,random_lower_zimu,random_upper_zimu])
draw.text([5+i*24,10],random_char,(fandomColor()),font=font)
valid_list.append(random_char)
for i in range(100):
draw.point([random.randint(0, 5+i*24), random.randint(0,5+i*24 )], fill=fandomColor())
f=BytesIO()
img.save(f,"png")
data=f.getvalue()
valid_str="".join(valid_list)
print(valid_str)
request.session["keepValidCode"]=valid_str
return HttpResponse(data)
#方式一
#通过静态文件的目录来定位路径
import os
path = os.path.join(settings.BASE_DIR,"static","img","ss.jpg") #BASE_DIR表示settings的上一级目录的上一级目录
with open(path,"rb") as f:
data=f.read()
#方式二
from PIL import Image #导入PIL模块
img=Image.new(mode="RGB",size=(120,40),color="yellow") #创建画笔
f=open("validCode.png","wb") #保存在本地
img.save(f,"png")
with open("validCode.png","rb") as f: #进行读取
data=f.read()
return HttpResponse(data) #然后返回给前端
'''
缺点:
会在服务端的根目录下产生一个文件,我们不应该让它产生文件
'''
#方式三
'''
把文件读到内存中去
'''
from io import BytesIO
from PIL import Image
img=Image.new(mode="RGB",size=(120,40),color="blue")
f=BytesIO()
img.save(f,"png")
data=f.getvalue()
return HttpResponse(data)
#方式四
'''
随机产生验证码
'''
# def text(self, xy, text, fill=None, font=None, anchor=None,*args, **kwargs):
from io import BytesIO #把内容保存到内存中
import random
from PIL import Image,ImageDraw,ImageFont #导入图画,画笔,字体
img = Image.new(mode="RGB", size=(120, 40), color=(random.randint(0,255),random.randint(0,255),random.randint(0,255)))
draw=ImageDraw.Draw(img,"RGB")
font=ImageFont.truetype("blog/static/font/kumo.ttf",25)
def fandomColor():
'''
生成随机颜色
:return:
'''
random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)
valid_list=[]
for i in range(5):
random_num=str(random.randint(0,9))
random_lower_zimu=chr(random.randint(65,90))
random_upper_zimu=chr(random.randint(97,122))
random_char=random.choice([random_num,random_lower_zimu,random_upper_zimu])
draw.text([5+i*24,10],random_char,(fandomColor()),font=font)
valid_list.append(random_char)
for i in range(100):
draw.point([random.randint(0, 5+i*24), random.randint(0,5+i*24 )], fill=fandomColor())
f=BytesIO()
img.save(f,"png")
data=f.getvalue()
valid_str="".join(valid_list)
print(valid_str)
request.session["keepValidCode"]=valid_str
return HttpResponse(data)
验证码的局部刷新

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.min.css">
<script src="/static/jquery-3.2.1.js"></script>
<script src="/static/bootstrap-3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form class="form-horizontal">
{% csrf_token %}
<div class="form-group">
<label for="username" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="username" placeholder="用户名">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-4">
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="validCode" class="col-sm-2 control-label">验证码</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="validCode" placeholder="验证码">
<img src="/get_verification_img/" alt="" class="valid_code_img" >
<a class="refresh">刷新</a>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default signlogin">Sign in</button>
</div>
<div class="col-sm-offset-2 col-sm-10">
</div>
</div>
</form>
<script>
$(".refresh").click(function () {
$(".valid_code_img")[0].src+="?";
});
$("img").click(function () {
$(this)[0].src+="?";
});
</script>
x
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<link rel="stylesheet" href="/static/bootstrap-3.3.7/css/bootstrap.min.css">
<script src="/static/jquery-3.2.1.js"></script>
<script src="/static/bootstrap-3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<form class="form-horizontal">
{% csrf_token %}
<div class="form-group">
<label for="username" class="col-sm-2 control-label">用户名</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="username" placeholder="用户名">
</div>
</div>
<div class="form-group">
<label for="inputPassword3" class="col-sm-2 control-label">Password</label>
<div class="col-sm-4">
<input type="password" class="form-control" id="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<label for="validCode" class="col-sm-2 control-label">验证码</label>
<div class="col-sm-4">
<input type="text" class="form-control" id="validCode" placeholder="验证码">
<img src="/get_verification_img/" alt="" class="valid_code_img" >
<a class="refresh">刷新</a>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-default signlogin">Sign in</button>
</div>
<div class="col-sm-offset-2 col-sm-10">
</div>
</div>
</form>
<script>
$(".refresh").click(function () {
$(".valid_code_img")[0].src+="?";
});
$("img").click(function () {
$(this)[0].src+="?";
});
</script>
django-生成随机验证码的更多相关文章
- Django中生成随机验证码(pillow模块的使用)
Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...
- Java生成随机验证码
package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...
- 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 ...
- Python使用PIL模块生成随机验证码
PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...
- C#生成随机验证码例子
C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...
- pillow实例 | 生成随机验证码
1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...
- struts2生成随机验证码图片
之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...
- python模块之PIL模块(生成随机验证码图片)
PIL简介 什么是PIL PIL:是Python Image Library的缩写,图像处理的模块.主要的类包括Image,ImageFont,ImageDraw,ImageFilter PIL的导入 ...
- C#生成随机验证码
使用YZMHelper帮助类即可 using System; using System.Web; using System.Drawing; using System.Security.Cryptog ...
随机推荐
- [ACM] poj 2017 Speed Limit
Speed Limit Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 17030 Accepted: 11950 Des ...
- Java精选笔记_Java API
String类 String类的初始化 String是一个特殊的对象,一旦被初始化,就不会被改变 1.使用字符串常量直接初始化一个String对象 String s1="abc" ...
- 连接oracle服务器超慢--原因分析
连接oracle服务器超慢:有如下原因可能会影响. 网络不好:oracle服务器跟本地网络不好. oracle服务器内存不足:导致反应超慢 监听日志listener.log太大:导致响应超慢. 所以对 ...
- list中的比较
一说到list的的确不知道写些什么.....我觉得别人总结的比我写的还要好很多. 我擅长记录自己的误区. |--List:元素是有序的(怎么存的就怎么取出来,顺序不会乱),元素可以重复(角标1上有个3 ...
- list的下标【python】
转自:http://www.cnblogs.com/dyllove98/archive/2013/07/20/3202785.html list的下表从零开始,和C语言挺类似的,但是增加了负下标的使用 ...
- js中解析json时候的eval和$.parseJSON()的区别以及JSON.stringify()
1.第一个区别是:安全性 json格式非常受欢迎,而解析json的方式通常用JSON.parse()但是eval()方法也可以解析,这两者之间有什么区别呢? JSON.parse()之可以解 ...
- 进程防结束之PS_CROSS_THREAD_FLAGS_SYSTEM
有人投到黑防去了,不过黑防不厚道,竟然没给完整的代码,自己整理一份备用吧,驱网.DebugMan.邪八的那群人直接飘过吧. 这种方法的关键在于给线程的ETHREAD.CrossThreadFlags设 ...
- .net 防盗链
Global.asax 文件中 protected void Application_BeginRequest(object sender, EventArgs e) { //判断当前请求是否是访问 ...
- android基础---->发送和接收短信
收发短信应该是每个手机最基本的功能之一了,即使是许多年前的老手机也都会具备这项功能,而Android 作为出色的智能手机操作系统,自然也少不了在这方面的支持.今天我们开始自己创建一个简单的发送和接收短 ...
- Android 查看每个应用的最大可用内存
http://blog.csdn.net/vshuang/article/details/39647167 Android 内存管理 &Memory Leak & OOM 分析 ...