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('')
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.*.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教程:随机验证码生成和join 字符串
函数:string.join() Python中有join()和os.path.join()两个函数,具体作用如下: join(): 连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符) ...
- PHP 验证码生成类(可定制长度和内容)
===================VerifyTool====================== <?php class VerifyTool { private $fontPath; / ...
- python登录验证码生成及自动化测试规避
在用django写论坛的时候,需要有登录及注册功能. 故就登录界面后端需要生成随机验证码并传值给前端的代码进行编写如下. 验证码生成png需要调用到python的图形库 生成注册码img import ...
- Python识别网站验证码
http://drops.wooyun.org/tips/6313 Python识别网站验证码 Manning · 2015/05/28 10:57 0x00 识别涉及技术 验证码识别涉及很多方面的内 ...
- 开发工具类API调用的代码示例合集:六位图片验证码生成、四位图片验证码生成、简单验证码识别等
以下示例代码适用于 www.apishop.net 网站下的API,使用本文提及的接口调用代码示例前,您需要先申请相应的API服务. 六位图片验证码生成:包括纯数字.小写字母.大写字母.大小写混合.数 ...
- python的random模块(生成验证码)
python的random模块(生成验证码) random模块常用方法 random.random() #生成0到1之间的随机数,没有参数,float类型 random.randint(1, 3) # ...
- python习题——随机整数生成类
随机整数生成类 可以先设定一批生成数字的个数,可设定指定生成的数值的范围 1.普通类实现 import random import random class RandomGen: def __init ...
- Python 常用模块系列学习(1)--random模块常用function总结--简单应用--验证码生成
random模块--random是一个生成器 首先: import random #导入模块 print (help(random)) #打印random模块帮助信息 常用function ...
- SpringBoot实现网站注册,邮件激活码激活功能
项目源码:https://gitee.com/smfx1314/springbootemail 上一篇文章已经讲到如何springboot如何实现邮件的发送,趁热打铁,这篇文章实现如下功能. 很多网站 ...
随机推荐
- ambari
http://blog.csdn.net/ggz631047367/article/details/50491616 https://cwiki.apache.org/confluence/displ ...
- springboot+swagger2
springboot+swagger2 小序 新公司的第二个项目,是一个配置管理终端机(比如:自动售卖机,银行取款机)的web项目,之前写过一个分模块的springboot框架,就在那个框架基础上进行 ...
- HDU-3032
Problem Description Nim is a two-player mathematic game of strategy in which players take turns remo ...
- Microsoft Visual Studio调试监视器(MSVSMON.EXE)未能启动
在启动VS2010项目时,遇到如图所示"Microsoft Visual Studio调试监视器(MSVSMON.EXE)未能启动"的问题. 原因是VS2010安装路径(我的是D: ...
- php字符的替换,截取,指定查找
<?php/** * Created by 郭鹏. * User: msi * Date: 2017/9/27 * Time: 14:17 *///随机数生成器echo rand();echo ...
- vue路由跳转 vue-router的使用
1.路由对象和路由匹配 路由对象,即$router会被注入每个组件中,可以利用它进行一些信息的获取.如 属性 说明 $route.path 当前路由对象的路径,如'/view/a' $rotue.pa ...
- asp.net mvc项目实记-开启伪静态-Bundle压缩css,js
百度这些东西,还是会浪费了一些不必要的时间,记录记录以备后续 一.开启伪静态 如果不在web.config中配置管道开关则伪静态无效 首先在RouteConfig.cs中中注册路由 routes.Ma ...
- 【Python练习1】统计一串字符中英文字母、空格、数字和其他字符的个数
练习思路: 1.输入一串字符 2.筛选出字符中的英文字母并统计 3.筛选出字符中的空格并统计 4.筛选出字符中的数字并统计 5.筛选出字符中的其他字符并统计 代码实现: def msg(s): abc ...
- 初识HBase
现如今,分布式架构大行其道,实际项目中使用HBase也是比比皆是.虽说自己在分布式方面接触甚少,但作为程序猿还是需要不断的给自己充电的.网上搜索了一些教程,还是觉得<HBase权威指南>不 ...
- 使用vs编译事件来动态发布配置文件
我们知道开发有很多的环境,一般我们会分为开发环境,测试环境,生产环境.而我们使用的vs默认配置就两种:Debug和Release.当然vs支持通过配置管理器来添加,编辑及删除配置. 为此不同的环境和配 ...