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 18 Python
PyQt 线程类 QThread使用详解
Jul 16 Python
Python3.6连接Oracle数据库的方法详解
May 18 Python
python 脚本生成随机 字母 + 数字密码功能
May 26 Python
python opencv读mp4视频的实例
Dec 07 Python
python实现淘宝秒杀脚本
Jun 23 Python
django配置连接数据库及原生sql语句的使用方法
Mar 03 Python
5款Python程序员高频使用开发工具推荐
Apr 10 Python
python word转pdf代码实例
Aug 16 Python
对python while循环和双重循环的实例详解
Aug 23 Python
PyTorch中torch.tensor与torch.Tensor的区别详解
May 18 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 $_SERVER详解
2009/01/16 PHP
细谈php中SQL注入攻击与XSS攻击
2012/06/10 PHP
Laravel5权限管理方法详解
2016/07/26 PHP
thinkPHP商城公告功能开发问题分析
2016/12/01 PHP
jquery uaMatch源代码
2011/02/14 Javascript
JavaScript阻止浏览器返回按钮的方法
2015/03/18 Javascript
全面了解addEventListener和on的区别
2016/07/14 Javascript
关于Jquery中的bind(),on()绑定事件方式总结
2016/10/26 Javascript
bootstrapValidator表单验证插件学习
2016/12/30 Javascript
jQuery实现简单的回到顶部totop功能示例
2017/10/16 jQuery
Vue组件Draggable实现拖拽功能
2018/12/01 Javascript
prettier自动格式化去换行的实现代码
2020/08/25 Javascript
vue实现简易的双向数据绑定
2020/12/29 Vue.js
[55:44]完美世界DOTA2联赛决赛 FTD vs Phoenix 第二场 11.08
2020/11/11 DOTA
Python之os操作方法(详解)
2017/06/15 Python
pandas Dataframe行列读取的实例
2018/06/08 Python
Django安装配置mysql的方法步骤
2018/10/15 Python
Python OpenCV中的resize()函数的使用
2019/06/20 Python
Python空间数据处理之GDAL读写遥感图像
2019/08/01 Python
python生成器用法实例详解
2019/11/22 Python
python使用建议与技巧分享(二)
2020/08/17 Python
利用Python实现Json序列化库的方法步骤
2020/09/09 Python
意大利在线大学图书馆:Libreria universitaria
2019/07/16 全球购物
超市营业员求职简历的自我评价
2013/10/17 职场文书
自我鉴定三原则
2014/01/13 职场文书
幼儿园母亲节活动方案
2014/03/10 职场文书
中国梦演讲稿教师篇
2014/04/23 职场文书
2014年党员加强作风建设思想汇报
2014/09/15 职场文书
教师见习报告范文
2014/11/03 职场文书
2014年办公室工作总结范文
2014/11/12 职场文书
煤矿安全保证书
2015/02/27 职场文书
会计工作态度自我评价
2015/03/06 职场文书
升职感谢领导的话语及升职感谢信
2019/06/24 职场文书
2019大学生暑期实习心得总结
2019/08/21 职场文书
小程序后台PHP版本部署运行 LNMP+WNMP
2021/04/01 Servers
浅谈redis的过期时间设置和过期删除机制
2022/03/18 MySQL