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模块——生成随机验证码的更多相关文章

  1. Python使用PIL模块生成随机验证码

    PIL模块的安装 pip3 install pillow 生成随机验证码图片 import random from PIL import Image, ImageDraw, ImageFont fro ...

  2. pillow实例 | 生成随机验证码

    1 PIL(Python Image Library) PIL是Python进行基本图片处理的package,囊括了诸如图片的剪裁.缩放.写入文字等功能.现在,我便以生成随机验证码为例,讲述PIL的基 ...

  3. 基于Python Pillow库生成随机验证码

    from PIL import Image from PIL import ImageDraw from PIL import ImageFont import random class ValidC ...

  4. Django中生成随机验证码(pillow模块的使用)

    Django中生成随机验证码 1.html中a标签的设置 <img src="/get_validcode_img/" alt=""> 2.view ...

  5. Python 生成随机验证码

    Python生成随机验证码  Python生成随机验证码,需要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创建图片 1 2 3 4 5 6 7 8 9 fro ...

  6. Python生成随机验证码

    Python生成随机验证码,需要使用PIL模块. 安装: pip3 install pillow 基本使用 1.创建图片 from PIL import Image img = Image.new(m ...

  7. Java生成随机验证码

    package com.tg.snail.core.util; import java.awt.Color; import java.awt.Font; import java.awt.Graphic ...

  8. C#生成随机验证码例子

    C#生成随机验证码例子: 前端: <tr> <td width=" align="center" valign="top"> ...

  9. struts2生成随机验证码图片

    之前想做一个随机验证码的功能,自己也搜索了一下别人写的代码,然后自己重新用struts2实现了一下,现在将我自己实现代码贴出来!大家有什么意见都可以指出来! 首先是生成随机验证码图片的action: ...

  10. 使用模块PIL 生成 随机验证码

    --------------默认自己无能,无疑是给失败制造机会!你认为自己是什么样的人,就将成为什么样的人. 要使用PIL模块. 安装: 1 pip3 install pillow 基本使用 1. 创 ...

随机推荐

  1. DNS CNAME limitations cname 在哪些情况下不能配置

    https://www.rfc-editor.org/rfc/rfc1912.html https://www.rfc-editor.org/rfc/rfc2181.html 说明: domain n ...

  2. 微信小程序中如何设置跳转页面

    修改project.config.json内容 "cloudfunctionRoot":"cloud", //配置云开发的路径 更改app.js文件内容 App ...

  3. 1.3 ODBC 部署监控数据库

    一.安装ODBC 来自为知笔记(Wiz)

  4. 软件工程日报——第十天(Android 开发中的异常处理问题)

    Android 开发中的异常处理问题 在代码的编写工作当中,我们会遇到很多有关错误处理的内容.这个时候,你用的最多的应该是try-catch-finally,这样的句式.系统提供的这个句式极大方便我们 ...

  5. 这篇文章汇聚33个BUG!来挑战一下,看看你能找出来几个?

    你好呀,我是歪歪. 前几天看到"Qunar技术沙龙"公众号推送了一篇关于他们举办了一场"Code Review大赛"的文章. 看到 Code Review 我很 ...

  6. 某大厂面试题:说一说Java、Spring、Dubbo三者SPI机制的原理和区别

    大家好,我是三友~~ 今天来跟大家聊一聊Java.Spring.Dubbo三者SPI机制的原理和区别. 其实我之前写过一篇类似的文章,但是这篇文章主要是剖析dubbo的SPI机制的源码,中间只是简单地 ...

  7. Sping Security前后端分离两种方案

    前言 本篇文章是基于Spring Security实现前后端分离登录认证及权限控制的实战,主要包括以下四方面内容: Spring Seciruty简单介绍: 通过Spring Seciruty实现的基 ...

  8. 全网最详细中英文ChatGPT-GPT-4示例文档-人工智能助手从0到1快速入门——官网推荐的48种最佳应用场景(附python/node.js/curl命令源代码,小白也能学)

    目录 Introduce 简介 setting 设置 Prompt 提示 Sample response 回复样本 API request 接口请求 python接口请求示例 node.js接口请求示 ...

  9. LeeCode 433 最小基因变化

    LeeCode 433 最小基因变化 题目描述: 基因序列可以表示为一条由 8 个字符组成的字符串,其中每个字符都是 'A'.'C'.'G' 和 'T' 之一. 假设我们需要调查从基因序列 start ...

  10. Android Activity的事件分发机制-源码解析

    查看Activity的dispatchTouchEvent方法: public boolean dispatchTouchEvent(MotionEvent ev) { if (ev.getActio ...