Python实现网站注册验证码生成类

作者:就今夜 时间:2023-10-24 13:58:02 

本文实例为大家分享了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,注册,验证码
0
投稿

猜你喜欢

  • Python中and和or如何使用

    2022-04-27 18:01:49
  • W3C优质网页小贴士(三)

    2008-04-09 13:32:00
  • 对Python 除法负数取商的取整方式详解

    2023-10-02 20:21:41
  • MySQL之批量插入的4种方案总结

    2024-01-19 16:13:11
  • numpy 进行数组拼接,分别在行和列上合并的实例

    2021-06-28 00:06:40
  • 他们是如何不让我的Teleport和Webzip工作的?

    2010-07-14 21:06:00
  • 试了下Golang实现try catch的方法

    2023-07-21 20:49:59
  • 详解Angular之constructor和ngOnInit差异及适用场景

    2024-05-11 09:18:16
  • 基于Pyinstaller打包Python程序并压缩文件大小

    2023-11-10 06:41:05
  • 技巧和诀窍:用Silverlight支持全屏模式

    2007-09-23 12:37:00
  • 深入讲解HTTPS中的加密算法

    2023-01-20 18:10:52
  • Tensorflow 2.4加载处理图片的三种方式详解

    2023-12-07 05:28:26
  • SQL Server 数据库管理常用的SQL和T-SQL语句

    2024-01-27 22:01:09
  • 对numpy中array和asarray的区别详解

    2022-06-26 04:40:12
  • pandas学习之df.fillna的具体使用

    2023-10-16 20:34:08
  • 在原窗口还是新窗口打开链接?

    2009-12-07 21:24:00
  • 关于Numpy中的行向量和列向量详解

    2021-06-29 20:30:48
  • Keepalived+HAProxy实现MySQL高可用负载均衡的配置

    2024-01-18 23:08:52
  • SQL基础语句总结

    2011-03-11 14:54:00
  • ubunt18.04LTS+vscode+anaconda3下的python+C++调试方法

    2023-04-01 16:46:37
  • asp之家 网络编程 m.aspxhome.com