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实现根据指定端口探测服务器/模块部署的方法
Aug 25 Python
使用Python设置tmpfs来加速项目的教程
Apr 17 Python
python实现DES加密解密方法实例详解
Jun 30 Python
实例Python处理XML文件的方法
Aug 31 Python
Windows下搭建python开发环境详细步骤
Jul 20 Python
python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍
Sep 21 Python
python3中set(集合)的语法总结分享
Mar 24 Python
解决sublime+python3无法输出中文的问题
Dec 12 Python
关于pytorch多GPU训练实例与性能对比分析
Aug 19 Python
Python Celery多队列配置代码实例
Nov 22 Python
利用python控制Autocad:pyautocad方式
Jun 01 Python
利用python对mysql表做全局模糊搜索并分页实例
Jul 12 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与已存在的Java应用程序集成
2006/10/09 PHP
php ob_flush,flush在ie中缓冲无效的解决方法
2010/05/09 PHP
php实现的任意进制互转类分享
2015/07/07 PHP
php获取'/'传参的值简单方法
2017/07/13 PHP
PHP切割汉字的常用方法实例总结
2019/04/27 PHP
JavaScript的9个陷阱及评点分析
2008/05/16 Javascript
子窗口、父窗口和Silverlight之间的相互调用
2010/08/16 Javascript
深入理解JavaScript系列(4) 立即调用的函数表达式
2012/01/15 Javascript
js实现日历可获得指定日期周数及星期几示例分享(js获取星期几)
2014/03/14 Javascript
jquery实现具有收缩功能的垂直导航菜单
2016/02/16 Javascript
Angular.js中下拉框实现渲染html的方法
2017/06/18 Javascript
基于JSONP原理解析(推荐)
2017/12/04 Javascript
微信小程序button组件使用详解
2018/01/31 Javascript
[03:24]DOTA2超级联赛专访hao 大翻盘就是逆袭
2013/05/24 DOTA
[10:53]2018DOTA2国际邀请赛寻真——EG
2018/08/11 DOTA
Python实例一个类背后发生了什么
2016/02/09 Python
windows10系统中安装python3.x+scrapy教程
2016/11/08 Python
基于Python对象引用、可变性和垃圾回收详解
2017/08/21 Python
Python实现发送与接收邮件的方法详解
2018/03/28 Python
Python面向对象之Web静态服务器
2019/09/03 Python
python解析多层json操作示例
2019/12/30 Python
python删除某个目录文件夹的方法
2020/05/26 Python
在pycharm中创建django项目的示例代码
2020/05/28 Python
简单了解Python多态与属性运行原理
2020/06/15 Python
Python错误的处理方法
2020/06/23 Python
python中Django文件上传方法详解
2020/08/05 Python
解决HTML5手机端页面缩放的问题
2017/10/27 HTML / CSS
什么叫应用程序域?什么是托管代码?什么是强类型系统?什么是装箱和拆箱?什么是重载?CTS、CLS和CLR分别作何解释?
2012/05/23 面试题
法制宣传口号
2014/06/16 职场文书
商务英语专业毕业生求职信
2014/07/06 职场文书
湖南省召开党的群众路线教育实践活动总结大会报告
2014/10/21 职场文书
任命通知范文
2015/04/21 职场文书
2015年生活老师工作总结
2015/05/27 职场文书
村主任当选感言
2015/08/01 职场文书
小学语文课《掌声》教学反思
2016/03/03 职场文书
2016年小学“感恩教师”主题队日活动总结
2016/04/01 职场文书