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实现的jpg格式图片修复代码
Apr 21 Python
Python扫描IP段查看指定端口是否开放的方法
Jun 09 Python
利用python代码写的12306订票代码
Dec 20 Python
用Python写一个无界面的2048小游戏
May 24 Python
TensorFlow模型保存/载入的两种方法
Mar 08 Python
Python3导入自定义模块的三种方法详解
Apr 13 Python
python3+PyQt5+Qt Designer实现扩展对话框
Apr 20 Python
python3读取csv和xlsx文件的实例
Jun 22 Python
python集合比较(交集,并集,差集)方法详解
Sep 13 Python
解决python3运行selenium下HTMLTestRunner报错的问题
Dec 27 Python
Python 实现网课实时监控自动签到、打卡功能
Mar 12 Python
基于Python实现nc批量转tif格式
Aug 14 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生成静态页面详解
2006/12/05 PHP
PHP 出现乱码和Sessions验证问题的解决方法!
2008/12/06 PHP
使用Discuz关键词服务器实现PHP中文分词
2014/03/11 PHP
JavaScript 仿关机效果的图片层
2008/12/26 Javascript
js自动生成的元素与页面原有元素发生堆叠的解决方法
2013/10/24 Javascript
javascript原生和jquery库实现iframe自适应高度和宽度
2014/07/18 Javascript
javascript将DOM节点添加到文档的方法实例分析
2015/08/04 Javascript
基于jquery实现瀑布流布局
2020/06/28 Javascript
JavaScript知识点总结(十)之this关键字
2016/05/31 Javascript
javascript 显示全局变量与隐式全局变量的区别
2017/02/09 Javascript
详解如何将angular-ui的图片轮播组件封装成一个指令
2017/05/09 Javascript
详解webpack+vue-cli项目打包技巧
2017/06/17 Javascript
layui之select的option叠加问题的解决方法
2018/03/08 Javascript
Vue render深入开发讲解
2018/04/13 Javascript
详解vue指令与$nextTick 操作DOM的不同之处
2018/08/02 Javascript
解决angularjs WdatePicker ng-model的问题
2018/09/13 Javascript
Vue.js中该如何自己维护路由跳转记录
2019/05/19 Javascript
在Django的模型中执行原始SQL查询的方法
2015/07/21 Python
Python文本相似性计算之编辑距离详解
2016/11/28 Python
python使用PIL模块获取图片像素点的方法
2019/01/08 Python
快速解决pyqt5窗体关闭后子线程不同时退出的问题
2019/06/19 Python
Python爬虫 urllib2的使用方法详解
2019/09/23 Python
Anaconda和ipython环境适配的实现
2020/04/22 Python
python 制作python包,封装成可用模块教程
2020/07/13 Python
Python requests及aiohttp速度对比代码实例
2020/07/16 Python
Python 爬虫批量爬取网页图片保存到本地的实现代码
2020/12/24 Python
python 实现百度网盘非会员上传超过500个文件的方法
2021/01/07 Python
大学三年的自我评价
2013/12/25 职场文书
2014庆六一活动方案
2014/03/02 职场文书
公司业务员岗位职责
2014/03/18 职场文书
学校社会实践活动总结
2014/07/03 职场文书
公务员党的群众路线教育实践活动学习心得体会
2014/10/30 职场文书
刑事上诉状(量刑过重)
2015/05/23 职场文书
单位工资证明范本
2015/06/12 职场文书
2019年大学毕业生个人自我鉴定范文大全
2019/03/21 职场文书
手把手教你使用TensorFlow2实现RNN
2021/07/15 Python