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文件及目录操作实例详解
Jun 04 Python
python检测是文件还是目录的方法
Jul 03 Python
python导入csv文件出现SyntaxError问题分析
Dec 15 Python
python实现音乐下载器
Apr 15 Python
python将回车作为输入内容的实例
Jun 23 Python
详解Python函数式编程—高阶函数
Mar 29 Python
python爬虫 基于requests模块的get请求实现详解
Aug 20 Python
python 定义类时,实现内部方法的互相调用
Dec 25 Python
Python爬虫库BeautifulSoup获取对象(标签)名,属性,内容,注释
Jan 25 Python
Python字典实现伪切片功能
Oct 28 Python
Python爬虫之Selenium警告框(弹窗)处理
Dec 04 Python
Pytorch中expand()的使用(扩展某个维度)
Jul 15 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 一元分词算法
2009/11/30 PHP
解析PHP生成静态html文件的三种方法
2013/06/18 PHP
workerman结合laravel开发在线聊天应用的示例代码
2018/10/30 PHP
PHP下载文件函数与用法示例
2019/09/27 PHP
CSS+jQuery实现的一个放大缩小动画效果
2013/09/24 Javascript
jquery next nextAll nextUntil siblings的区别介绍
2013/10/05 Javascript
javascript获取元素CSS样式代码示例
2013/11/28 Javascript
javascript break指定标签打破多层循环示例
2014/01/20 Javascript
JQuery中DOM事件合成用法实例分析
2015/06/13 Javascript
JavaScript资源预加载组件和滑屏组件的使用推荐
2016/03/10 Javascript
jQuery选择器基础入门教程
2016/05/10 Javascript
bootstrap table 数据表格行内修改的实现代码
2017/02/13 Javascript
微信小程序如何获知用户运行小程序的场景教程
2017/05/17 Javascript
JS使用正则表达式找出最长连续子串长度
2017/10/26 Javascript
js+css实现红包雨效果
2018/07/12 Javascript
jQuery实现的模仿雨滴下落动画效果
2018/12/11 jQuery
微信小程序实现打卡签到页面
2020/09/21 Javascript
[01:20]2018DOTA2亚洲邀请赛总决赛战队Mineski晋级之路
2018/04/07 DOTA
Python贪吃蛇游戏编写代码
2020/10/26 Python
Python实现通过文件路径获取文件hash值的方法
2017/04/29 Python
python中plot实现即时数据动态显示方法
2018/06/22 Python
解决新django中的path不能使用正则表达式的问题
2018/12/18 Python
Python数据可视化教程之Matplotlib实现各种图表实例
2019/01/13 Python
python3中pip3安装出错,找不到SSL的解决方式
2019/12/12 Python
Pytorch使用MNIST数据集实现基础GAN和DCGAN详解
2020/01/10 Python
Python在终端通过pip安装好包以后在Pycharm中依然无法使用的问题(三种解决方案)
2020/03/10 Python
python由已知数组快速生成新数组的方法
2020/04/08 Python
10 套华丽的CSS3 按钮小结
2012/10/03 HTML / CSS
The Hut德国站点:时装、家居用品、美容等
2016/09/23 全球购物
中文系师范生自荐信
2013/10/01 职场文书
2013英文求职信模板范文
2013/11/15 职场文书
日语专业个人求职信范文
2014/02/02 职场文书
《曹刿论战》教学反思
2014/03/02 职场文书
酒店优秀员工事迹材料
2014/06/02 职场文书
创业者如何撰写出一份打动投资人的商业计划书?
2019/07/02 职场文书
阿里云服务器搭建Php+Apache运行环境的详细过程
2021/05/15 PHP