catpcha
生成随机验证码:
# -*- coding: utf-8 -*-
# @Author: huangyong
# @Date: 2016-10-29 22:18:38
# @Last Modified by: Administrator
# @Last Modified time: 2016-11-21 20:08:31
import random
# pip install Pillow
# Image:是一个画板(context),ImageDraw:是一个画笔, ImageFont:画笔的字体
from PIL import Image, ImageDraw, ImageFont
import time
import os
import string # Captcha验证码 class Captcha(object):
# 把一些常量抽取成类属性
# 字体的位置
font_path = os.path.join(os.path.dirname(__file__), 'verdana.ttf')
# font_path = 'utils/captcha/verdana.ttf'
# 生成几位数的验证码
number = 4
# 生成验证码图片的宽度和高度
size = (100,40)
# 背景颜色,默认为白色 RGB(Re,Green,Blue)
bgcolor = (0,0,0)
# 随机字体颜色
random.seed(int(time.time()))
fontcolor = (random.randint(200,255),random.randint(100,255),random.randint(100,255))
# 验证码字体大小
fontsize = 20
# 随机干扰线颜色。
linecolor = (random.randint(0,250),random.randint(0,255),random.randint(0,250))
# 是否要加入干扰线
draw_line = True
# 是否绘制干扰点
draw_point = True
# 加入干扰线的条数
line_number = 3 # abcedf....ABCDEFG...Z
SOURCE = list(string.ascii_letters)
for index in range(0, 10):
SOURCE.append(str(index)) # 用来随机生成一个字符串(包括英文和数字)
# 定义成类方法,然后是私有的,对象在外面不能直接调用
@classmethod
def gene_text(cls):
return ''.join(random.sample(cls.SOURCE,cls.number))#number是生成验证码的位数 # 用来绘制干扰线
@classmethod
def __gene_line(cls,draw,width,height):
begin = (random.randint(0, width), random.randint(0, height))
end = (random.randint(0, width), random.randint(0, height))
draw.line([begin, end], fill = cls.linecolor) # 用来绘制干扰点
@classmethod
def __gene_points(cls,draw,point_chance,width,height):
chance = min(100, max(0, int(point_chance))) # 大小限制在[0, 100]
for w in range(width):
for h in range(height):
tmp = random.randint(0, 100)
if tmp > 100 - chance:
draw.point((w, h), fill=(0, 0, 0)) # 生成验证码
@classmethod
def gene_code(cls):
width,height = cls.size #宽和高
image = Image.new('RGBA',(width,height),cls.bgcolor) #创建图片
font = ImageFont.truetype(cls.font_path,cls.fontsize) #验证码的字体
draw = ImageDraw.Draw(image) #创建画笔
text = cls.gene_text() #生成字符串
font_width, font_height = font.getsize(text)
draw.text(((width - font_width) / 2, (height - font_height) / 2),text,font= font,fill=cls.fontcolor) #填充字符串
# 如果需要绘制干扰线
if cls.draw_line:
# 遍历line_number次,就是画line_number根线条
for x in range(0,cls.line_number):
cls.__gene_line(draw,width,height)
# 如果需要绘制噪点
if cls.draw_point:
cls.__gene_points(draw,10,width,height) return (text,image) # text验证码内容, image图片
得到的image不能直接传给前端,需要通过from io import BytesIO处理
def img_catpcha(request):
""" 调用catpcha生成方法生成随机验证码,将验证码写入字节流io.BytesTO,再读取出来。HttpResponse对象才能识别 """
text, image = hycaptcha.Captcha.gene_code()
# print(text)
out = BytesIO()
image.save(out, 'png') # 1.将image储存进BytesIO对象
out.seek(0) # 将光标制定到初始位置(读写的时候都会移动光标)
response = HttpResponse(content_type="image/png") # 申明对象类型
response.write(out.read()) # 2.将储存的image read出来并写入HttpResponse对象中,就可以使用HttpResponse对象将image传给前端页面
response['Content-length'] = out.tell() # 获取image的长度
return response
catpcha的更多相关文章
- PHP编写的图片验证码类文件分享方法
适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...
- js canvas captcha
js canvas captcha https://thejackalofjavascript.com/building-a-captcha-using-html5-canvas/ https://a ...
- 使用Python破解验证码
Keywords: python captcha Most people don’t know this but my honours thesis was about using a compute ...
随机推荐
- 【opencv基础】imread-第二个参数
问题1: 显示的是灰色的界面,不能正常显示图像. 解决方法:在imshow之后加上waitKey即可.原因here: Note:This function should be followed by ...
- TJU Problem 1065 Factorial
注意数据范围,十位数以上就可以考虑long long 了,断点调试也十分重要. 原题: 1065. Factorial Time Limit: 1.0 Seconds Memory Limit ...
- HDU 1002:A + B Problem II(大数相加)
A + B Problem II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- StyleCop 是什么,可以帮助团队带来什么价值?
StyleCop 本质上是一个 C# 源代码规则分析器,可以帮助团队成员强制执行一组代码样式和一致性规则. 本文将简述 StyleCop 以及它能为团队带来的价值. 本文内容 StyleCop 是什么 ...
- django所遇到问题简单总结
问题虽小,但却值得深思 一.改mysql密码 方法1: 用SET PASSWORD命令 首先登录MySQL. 格式:mysql> set password for 用户名@localhost = ...
- 【HAOI2016】食物链
HA真是弱…… 原题: 1.食物链[问题描述]如图所示为某生态系统的食物网示意图,据图回答第1小题. 1.数一数,在这个食物网中有几条食物链( )现在给你n个物种和m条能量流动关系,求其中的食物链条数 ...
- 2017年最新cocoapods安装教程(解决淘宝镜像源无效以及其他源下载慢问题)
首先,先来说一下一般的方法吧,就是把之前的淘宝源替换成一个可用的的源: 使用终端查看当前的源 gem sources -l gem sources -r https://rubygems.org/ # ...
- PDFSharp生成PDF (转)
http://www.cnblogs.com/zhouxin/p/3228108.html 在上面用OpenXML生成word后,原来利用Word2010里的导出成PDF功能就不能用. 然后找开源组件 ...
- proc文件系统漫谈
1. /proc/buddyinfo:/proc/buddyinfo是linuxbuddy系统管理物理内存的debug信息. 在linux中使用buddy算法解决物理内存的外碎片问题,其把所有空闲的内 ...
- Oracle误删除表空间的恢复
对于误删除表空间的恢复,本文通过基于数据库的时间点恢复和基于表空间的时间点恢复分别加以讨论 一 通过基于数据库的时间点恢复被误删除的表空间 1 需要注意的事项 a 基于数据库的时间点恢复将会回退整个数 ...