生成随机验证码:

# -*- 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的更多相关文章

  1. PHP编写的图片验证码类文件分享方法

    适用于自定义的验证码类! <?php/* * To change this license header, choose License Headers in Project Propertie ...

  2. js canvas captcha

    js canvas captcha https://thejackalofjavascript.com/building-a-captcha-using-html5-canvas/ https://a ...

  3. 使用Python破解验证码

    Keywords: python captcha Most people don’t know this but my honours thesis was about using a compute ...

随机推荐

  1. Audition CS6在Win 7中“音频输入的采样频率与输出设备不匹配”问题的解决 - imsoft.cnblogs

    1.运行Audion CS6,出现问题框. 2.右击任务栏右下方的那个小喇叭图标,在弹出的提示框里,单击“播放设备”.如图: 3.单击“播放设备”后,在图内左键单击“扬声器”然后双击“扬声器”,出现下 ...

  2. LeetCode Majority Element Python

    Given an array of size n, find the majority element. The majority element is the element that appear ...

  3. 转-spring-boot 注解配置mybatis+druid(新手上路)-http://blog.csdn.net/sinat_36203615/article/details/53759935

    spring-boot 注解配置mybatis+druid(新手上路) 转载 2016年12月20日 10:17:17 标签: sprinb-boot / mybatis / druid 10475 ...

  4. web 分享代码片段

    <div class="bshare-custom icon-medium-plus"><a title="分享到QQ空间" class=&q ...

  5. MySQL命令行--导入导出数据库

    MySQL命令行导出数据库:   1,进入MySQL目录下的bin文件夹:cd MySQL中到bin文件夹的目录 如我输入的命令行:cd C:\Program Files\MySQL\MySQL Se ...

  6. putty SSH tunnel function

    github & dynamic

  7. win10禁用自动更新服务

    win10禁用自动更新服务 按Win+R,打开运行,输入"services.msc"打开服务: 找到"Windows Update",选择属性,修改为禁用即可: ...

  8. winform 程序调用及参数调用

    调用程序: // 新轮廓 -> 调用轮廓扫描程序        private void toolStripMenuItem9_Click(object sender, EventArgs e) ...

  9. 联想服务器配置 RAID

    联想服务器配置 RAID BIOS 中配置 RAID 阵列卡 x3650 和 x3850 一.进入 RAID 1.在开机自检时按 F1 进入 UEFI 配置界面 2.选择 System Setting ...

  10. 关于 eclipse startexplorer插件 快速打开文件夹

    转自:http://basti1302.github.io/startexplorer/ Just drag-and-drop the button to the Eclipse menu bar t ...