Python实现网站注册验证码生成类


Posted in Python onJune 08, 2017

本文实例为大家分享了Python网站注册验证码生成类的具体代码,供大家参考,具体内容如下

# -*- coding:utf-8 -*-
'''
Created on 2017年4月7日

@author: Water
'''
import os
import random
import string
import sys
import math
from PIL import Image,ImageDraw,ImageFont,ImageFilter
from django.conf import settings

 
#字体的位置,不同版本的系统会有不同
font_path = os.path.join('/home/workspace/aofeiKart/static', 'fonts/monaco.ttf')#settings.STATIC_ROOT, 'fonts/MONACO.TTF')
font_path = os.path.join(settings.STATIC_ROOT, 'fonts/monaco.ttf')
# print font_path
#生成几位数的验证码
number = 4
#生成验证码图片的高度和宽度
size = (100,30)
#背景颜色,默认为白色
bgcolor = (255,255,255)
#字体颜色,默认为蓝色
fontcolor = (0,0,255)
#干扰线颜色。默认为红色
linecolor = (255,0,0)
#是否要加入干扰线
draw_line = True
#加入干扰线条数的上下限
line_number = (1,5)
 
#用来随机生成一个字符串
# source = list(string.ascii_lowercase+'1234567890')
source = list('1234567890')
def gene_text():
#   return '6666'
  return ''.join(random.sample(source,number))#number是生成验证码的位数
#用来绘制干扰线
def gene_line(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 = linecolor)
 
#生成验证码
def gene_code():
  width,height = size #宽和高
  image = Image.new('RGBA',(width,height),bgcolor) #创建图片
  font = ImageFont.truetype(font_path,25) #验证码的字体
  draw = ImageDraw.Draw(image) #创建画笔
  text = gene_text() #生成字符串
  font_width, font_height = font.getsize(text)
  draw.text(((width - font_width) / number, (height - font_height)/number),text,
      font= font,fill=fontcolor) #填充字符串
  if draw_line:
    gene_line(draw,width,height)
  image = image.transform((width+20,height+10), Image.AFFINE, (1,-0.3,0,-0.1,1,0), Image.BILINEAR) #创建扭曲
  image = image.filter(ImageFilter.EDGE_ENHANCE_MORE) #滤镜,边界加强
  image_file = text+'.png'
  
  image_path = os.path.join(settings.STATIC_ROOT, 'images/%s'%image_file)

  image.save(image_path) #保存验证码图片
  
  return 'http://login.chaozu.net:8000/static/images/%s'%image_file, text

if __name__ == "__main__":
  print gene_code()

实现过程很简单,主要注意有2点:

1.安装PIL库,设置好字体保存目录

2.如果直接返回图片的二进制数据流的?,如下:

buf = io.BytesIO() #io.BytesIO() #io.StringIO() use it to fill str obj
image.save(buf, 'png')
request.session['captcha'] = text.lower() 

return HttpResponse(buf.getvalue(), 'image/png') # return the image data stream as image/jpeg format, browser will treat it as an image

 以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python调用cmd复制文件代码分享
Dec 27 Python
Python初学者需要注意的事项小结(python2与python3)
Sep 26 Python
python 利用pandas将arff文件转csv文件的方法
Feb 12 Python
由Python编写的MySQL管理工具代码实例
Apr 09 Python
Pandas实现dataframe和np.array的相互转换
Nov 30 Python
使用Python内置模块与函数进行不同进制的数的转换
Apr 26 Python
Python学习之os模块及用法
Jun 03 Python
matplotlib subplot绘制多个子图的方法示例
Jul 28 Python
Python字符串及文本模式方法详解
Sep 10 Python
Python grpc超时机制代码示例
Sep 14 Python
Python中常见的导入方式总结
May 06 Python
python中filter,map,reduce的作用
Jun 10 Python
Python实现多线程抓取网页功能实例详解
Jun 08 #Python
Python中with及contextlib的用法详解
Jun 08 #Python
Python使用pylab库实现画线功能的方法详解
Jun 08 #Python
Python实现对象转换为xml的方法示例
Jun 08 #Python
Python实现的手机号归属地相关信息查询功能示例
Jun 08 #Python
python用pickle模块实现“增删改查”的简易功能
Jun 07 #Python
Python3 socket同步通信简单示例
Jun 07 #Python
You might like
php 截取字符串并以零补齐str_pad() 函数
2011/05/07 PHP
php中最简单的字符串匹配算法
2014/12/16 PHP
php生成图片验证码
2015/06/09 PHP
PHP文件上传、客户端和服务器端加限制、抓取错误信息、完整步骤解析
2017/01/12 PHP
php+mysql实现的无限分类方法类定义与使用示例
2020/05/27 PHP
Js制作简单弹出层DIV在页面居中 中间显示遮罩的具体方法
2013/08/08 Javascript
尝试动手制作javascript放大镜效果
2015/12/25 Javascript
jQuery实现侧浮窗与中浮窗切换效果的方法
2016/09/05 Javascript
KnockoutJS 3.X API 第四章之表单value绑定
2016/10/10 Javascript
JS瀑布流实现方法实例分析
2016/12/19 Javascript
JS原型与原型链的深入理解
2017/02/15 Javascript
jQuery插件HighCharts绘制2D半圆环图效果示例【附demo源码下载】
2017/03/09 Javascript
详解Node.js串行化流程控制
2017/05/04 Javascript
JavaScript实现兼容IE6的收起折叠与展开效果实例
2017/09/20 Javascript
浅谈Angular2 模块懒加载的方法
2017/10/04 Javascript
基于Vuex无法观察到值变化的解决方法
2018/03/01 Javascript
vue-cli 目录结构详细讲解总结
2019/01/15 Javascript
[59:30]VG vs LGD 2019国际邀请赛淘汰赛 胜者组 BO3 第二场 8.22
2019/09/05 DOTA
python anaconda 安装 环境变量 升级 以及特殊库安装的方法
2017/06/21 Python
python matlibplot绘制3D图形
2018/07/02 Python
Django ManyToManyField 跨越中间表查询的方法
2018/12/18 Python
对Python中TKinter模块中的Label组件实例详解
2019/06/14 Python
python opencv minAreaRect 生成最小外接矩形的方法
2019/07/01 Python
python读取多层嵌套文件夹中的文件实例
2020/02/27 Python
python怎么对数字进行过滤
2020/07/05 Python
JAVA程序员自荐书
2014/01/30 职场文书
组织鉴定材料
2014/06/02 职场文书
就业意向书
2014/07/29 职场文书
忠诚奉献演讲稿
2014/09/12 职场文书
先进人物事迹材料
2014/12/29 职场文书
同学聚会通知短信
2015/04/20 职场文书
高一数学教学反思
2016/02/18 职场文书
Python基础之操作MySQL数据库
2021/05/06 Python
pytorch MSELoss计算平均的实现方法
2021/05/12 Python
opencv用VS2013调试时用Image Watch插件查看图片
2021/07/26 Python
SpringBoot整合阿里云视频点播的过程详解
2021/12/06 Java/Android