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中使用ConfigParser解析ini配置文件实例
Aug 30 Python
python itchat实现微信好友头像拼接图的示例代码
Aug 14 Python
Python3多线程操作简单示例
May 22 Python
解决在Python编辑器pycharm中程序run正常debug错误的问题
Jan 17 Python
详解python做UI界面的方法
Feb 27 Python
django中的图片验证码功能
Sep 18 Python
python 实现屏幕录制示例
Dec 23 Python
python中提高pip install速度
Feb 14 Python
python 爬虫基本使用——统计杭电oj题目正确率并排序
Oct 26 Python
弄清Pytorch显存的分配机制
Dec 10 Python
用python删除文件夹中的重复图片(图片去重)
May 12 Python
详解python的异常捕获
Mar 03 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实现Mysql读写分离
2013/06/28 PHP
ueditor 1.2.6 使用方法说明
2013/07/24 PHP
php的慢速日志引起的Mysql错误问题分析
2014/05/13 PHP
PHP魔术引号所带来的安全问题分析
2014/07/15 PHP
PHP采用自定义函数实现遍历目录下所有文件的方法
2014/08/19 PHP
php绘制圆形的方法
2015/01/24 PHP
php快速查找数据库中恶意代码的方法
2015/04/01 PHP
Codeigniter中集成smarty和adodb的方法
2016/03/04 PHP
php str_replace替换指定次数的方法详解
2017/05/05 PHP
PHP查询分页的实现代码
2017/06/09 PHP
PHP中OpenSSL加密问题整理
2017/12/14 PHP
firefox插件Firebug的使用教程
2010/01/02 Javascript
js escape,unescape解决中文乱码问题的方法
2010/05/26 Javascript
javascript处理表单示例(javascript提交表单)
2014/04/28 Javascript
基于jquery固定于顶部的导航响应浏览器滚动条事件
2014/11/02 Javascript
5个数组Array方法: indexOf、filter、forEach、map、reduce使用实例
2015/01/29 Javascript
在JavaScript中处理时间之setMinutes()方法的使用
2015/06/11 Javascript
jquery插件jquery.nicescroll实现图片无滚动条左右拖拽的方法
2015/08/10 Javascript
jquery实现带缩略图的可定制高度画廊效果(5种)
2015/08/28 Javascript
js实现一键复制功能
2017/03/16 Javascript
Angular4学习笔记之新建项目的方法
2017/07/18 Javascript
解读vue生成的文件目录结构及说明
2017/11/27 Javascript
vue插件draggable实现拖拽移动图片顺序
2018/12/01 Javascript
[03:28]2014DOTA2国际邀请赛 走近EG战队天才中单Arteezy
2014/07/12 DOTA
python和shell监控linux服务器的详细代码
2018/06/22 Python
Python 实现微信防撤回功能
2019/04/29 Python
使用PyInstaller将Pygame库编写的小游戏程序打包为exe文件及出现问题解决方法
2019/09/06 Python
python argparser的具体使用
2019/11/10 Python
Python HTTP下载文件并显示下载进度条功能的实现
2020/04/02 Python
HTML5安全介绍之内容安全策略(CSP)简介
2012/07/10 HTML / CSS
美术指导助理求职信
2014/04/20 职场文书
2014年最新领导班子整改方案
2014/09/27 职场文书
先进学校事迹材料
2014/12/30 职场文书
大学团日活动总结书
2015/05/11 职场文书
考试后的感想
2015/08/07 职场文书
pytorch 如何使用batch训练lstm网络
2021/05/28 Python