概述
图片验证码是验证码的一种,图片验证码常见的形式有输入图片中字母、数字等
作用
图片验证码是嵌入在企业网站、APP等应用中使用的一种验证方式,用户需要识别图片并按要求执行操作,在验证成功后才能使用某项功能。比如客户在某应用中发布文章或评论时,必须先完成图片验证,才可以进行发布操作 。一般用于最多为注册功能
模块安装
pip install pillow
实现
<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">verifycode</span><span class="hljs-params">(request)</span>:</span>
<span class="hljs-comment"># 引入绘图模块</span>
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image, ImageDraw, ImageFont
<span class="hljs-comment"># 引入随机函数模块</span>
<span class="hljs-keyword">import</span> random
<span class="hljs-comment"># 定义变量,用于画面的背景色、宽、高</span>
bgcolor = (random.randrange(<span class="hljs-number">20</span>, <span class="hljs-number">100</span>), random.randrange(
<span class="hljs-number">20</span>, <span class="hljs-number">100</span>), random.randrange(<span class="hljs-number">20</span>, <span class="hljs-number">100</span>))
width = <span class="hljs-number">100</span>
height = <span class="hljs-number">50</span>
<span class="hljs-comment"># 创建画面对象</span>
im = Image.new(<span class="hljs-string">'RGB'</span>, (width, height), bgcolor)
<span class="hljs-comment"># 创建画笔对象</span>
draw = ImageDraw.Draw(im)
<span class="hljs-comment"># 调用画笔的point()函数绘制噪点</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, <span class="hljs-number">100</span>):
xy = (random.randrange(<span class="hljs-number">0</span>, width), random.randrange(<span class="hljs-number">0</span>, height))
fill = (random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>), <span class="hljs-number">255</span>, random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>))
draw.point(xy, fill=fill)
<span class="hljs-comment"># 定义验证码的备选值</span>
str = <span class="hljs-string">'1234567890QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm'</span>
<span class="hljs-comment"># 随机选取4个值作为验证码</span>
rand_str = <span class="hljs-string">''</span>
<span class="hljs-keyword">for</span> i <span class="hljs-keyword">in</span> range(<span class="hljs-number">0</span>, <span class="hljs-number">4</span>):
rand_str += str[random.randrange(<span class="hljs-number">0</span>, len(str))]
<span class="hljs-comment"># 构造字体对象</span>
font = ImageFont.truetype(<span class="hljs-string">r'/home/xlg/PycharmProjects/fonts/ADOBEARABIC-BOLDITALIC.OTF'</span>, <span class="hljs-number">40</span>)
<span class="hljs-comment"># 构造字体颜色</span>
fontcolor1 = (<span class="hljs-number">255</span>, random.randrangea(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>), random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>))
fontcolor2 = (<span class="hljs-number">255</span>, random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>), random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>))
fontcolor3 = (<span class="hljs-number">255</span>, random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>), random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>))
fontcolor4 = (<span class="hljs-number">255</span>, random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>), random.randrange(<span class="hljs-number">0</span>, <span class="hljs-number">255</span>))
<span class="hljs-comment"># 绘制4个字</span>
draw.text((<span class="hljs-number">5</span>, <span class="hljs-number">2</span>), rand_str[<span class="hljs-number">0</span>], font=font, fill=fontcolor1)
draw.text((<span class="hljs-number">25</span>, <span class="hljs-number">2</span>), rand_str[<span class="hljs-number">1</span>], font=font, fill=fontcolor2)
draw.text((<span class="hljs-number">50</span>, <span class="hljs-number">2</span>), rand_str[<span class="hljs-number">2</span>], font=font, fill=fontcolor3)
draw.text((<span class="hljs-number">75</span>, <span class="hljs-number">2</span>), rand_str[<span class="hljs-number">3</span>], font=font, fill=fontcolor4)
<span class="hljs-comment"># 释放画笔</span>
<span class="hljs-keyword">del</span> draw
<span class="hljs-comment"># 存入session,用于做进一步验证</span>
request.session[<span class="hljs-string">'verify'</span>] = rand_str
<span class="hljs-comment"># 内存文件操作</span>
<span class="hljs-keyword">import</span> io
buf = io.BytesIO()
<span class="hljs-comment"># 将图片保存在内存中,文件类型为png</span>
im.save(buf, <span class="hljs-string">'png'</span>)
<span class="hljs-comment"># 将内存中的图片数据返回给客户端,MIME类型为图片png</span>
<span class="hljs-keyword">return</span> HttpResponse(buf.getvalue(), <span class="hljs-string">'image/png'</span>)
验证码使用
url(r'^getcode/$',main.verifycode,name='verifycode'),
<img src="/getcode/" onclick="this.src='https://www.zutuanxue.com/getcode/?id='+Math.random()">
配置settings
settings.py
EMAIL_HOST = os.environ.get(<span class="hljs-string">'MAIL_SERVER'</span>,<span class="hljs-string">'smtp.163.com'</span>)
EMAIL_HOST_USER = os.environ.get(<span class="hljs-string">'MAIL_USER'</span>,<span class="hljs-string">'15611833906@163.com'</span>)
EMAIL_HOST_PASSWORD = os.environ.get(<span class="hljs-string">'MAIL_PASSWORD'</span>,<span class="hljs-number">123456</span>)
单人邮件
<span class="hljs-keyword">from</span> django.core.mail <span class="hljs-keyword">import</span> send_mail
send_mail(
<span class="hljs-string">'Subject here'</span>,
<span class="hljs-string">'Here is the message.'</span>,
<span class="hljs-string">'from@example.com'</span>,
[<span class="hljs-string">'to@example.com'</span>],
fail_silently=<span class="hljs-keyword">False</span>,
)