黑松山资源网 Design By www.paidiu.com
本文实例为大家分享了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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
更新日志
2024年10月09日
2024年10月09日
- 【原神】V5.0攻略 | 林尼攻略一图流
- 李翊君.1993-相思的烈酒【上华】【WAV+CUE】
- 古巨基.1998-LEO.KU(国)【千禧年代】【WAV+CUE】
- 郭子.2001-原来你什么都不想要创作集丫滚石】【WAV+CUE】
- 《使命召唤:黑色行动6》新预告公布!10月25日发售
- Atlus《暗喻幻想》媒体评分汇总:高分好评如潮!
- 2024金摇杆奖提名揭晓 《黑神话》角逐最佳视觉设计!
- 群星《新说唱2024 第3期 (上)》[320K/MP3][32.76MB]
- 群星《新说唱2024 第3期 (上)》[FLAC/分轨][95.38MB]
- 群星《新说唱2024 第3期 (下)》[320K/MP3][31.36MB]
- 幻兽帕鲁手游什么时候正式上线 最新消息一览
- 西普大陆BOSS位置盘点 解锁天启纪元玩法
- 西普大陆精灵进阶培养攻略 精灵养成指南
- dnf手游法控法系职业哪个强 dnf手游法控法系职业强度排行
- 魔兽世界血藤护目镜图纸在哪买 wlk血藤护目镜图纸购买位置介绍