Python實(shí)現(xiàn)網(wǎng)站注冊(cè)驗(yàn)證碼生成類
本文實(shí)例為大家分享了Python網(wǎng)站注冊(cè)驗(yàn)證碼生成類的具體代碼,供大家參考,具體內(nèi)容如下
# -*- 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 #字體的位置,不同版本的系統(tǒng)會(huì)有不同 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 #生成幾位數(shù)的驗(yàn)證碼 number = 4 #生成驗(yàn)證碼圖片的高度和寬度 size = (100,30) #背景顏色,默認(rèn)為白色 bgcolor = (255,255,255) #字體顏色,默認(rèn)為藍(lán)色 fontcolor = (0,0,255) #干擾線顏色。默認(rèn)為紅色 linecolor = (255,0,0) #是否要加入干擾線 draw_line = True #加入干擾線條數(shù)的上下限 line_number = (1,5) #用來(lái)隨機(jī)生成一個(gè)字符串 # source = list(string.ascii_lowercase+'1234567890') source = list('1234567890') def gene_text(): # return '6666' return ''.join(random.sample(source,number))#number是生成驗(yàn)證碼的位數(shù) #用來(lái)繪制干擾線 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) #生成驗(yàn)證碼 def gene_code(): width,height = size #寬和高 image = Image.new('RGBA',(width,height),bgcolor) #創(chuàng)建圖片 font = ImageFont.truetype(font_path,25) #驗(yàn)證碼的字體 draw = ImageDraw.Draw(image) #創(chuàng)建畫(huà)筆 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) #創(chuàng)建扭曲 image = image.filter(ImageFilter.EDGE_ENHANCE_MORE) #濾鏡,邊界加強(qiáng) image_file = text+'.png' image_path = os.path.join(settings.STATIC_ROOT, 'images/%s'%image_file) image.save(image_path) #保存驗(yàn)證碼圖片 return 'http://login.chaozu.net:8000/static/images/%s'%image_file, text if __name__ == "__main__": print gene_code()
實(shí)現(xiàn)過(guò)程很簡(jiǎn)單,主要注意有2點(diǎn):
1.安裝PIL庫(kù),設(shè)置好字體保存目錄
2.如果直接返回圖片的二進(jìn)制數(shù)據(jù)流的話,如下:
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
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Python生成隨機(jī)驗(yàn)證碼的兩種方法
- Python隨機(jī)生成一個(gè)6位的驗(yàn)證碼代碼分享
- Python 隨機(jī)生成中文驗(yàn)證碼的實(shí)例代碼
- python生成驗(yàn)證碼圖片代碼分享
- python使用pil生成圖片驗(yàn)證碼的方法
- python PIL模塊與隨機(jī)生成中文驗(yàn)證碼
- python3 pillow生成簡(jiǎn)單驗(yàn)證碼圖片的示例
- Python random模塊(獲取隨機(jī)數(shù))常用方法和使用例子
- Python中random模塊生成隨機(jī)數(shù)詳解
- python的random模塊及加權(quán)隨機(jī)算法的python實(shí)現(xiàn)方法
- Python隨機(jī)數(shù)用法實(shí)例詳解【基于random模塊】
- Python實(shí)現(xiàn)簡(jiǎn)單生成驗(yàn)證碼功能【基于random模塊】
相關(guān)文章
django的模型類管理器——數(shù)據(jù)庫(kù)操作的封裝詳解
這篇文章主要介紹了django的模型類管理器——數(shù)據(jù)庫(kù)操作的封裝詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-04-04python實(shí)現(xiàn)超市掃碼儀計(jì)費(fèi)
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)超市掃碼儀計(jì)費(fèi),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05Async?IO在Python中的異步編程工作實(shí)例解析
這篇文章主要為大家介紹了Async?IO在Python中的異步編程工作實(shí)例解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-12-12pycharm創(chuàng)建一個(gè)python包方法圖解
在本篇文章中小編給大家分享了關(guān)于pycharm怎么創(chuàng)建一個(gè)python包的相關(guān)知識(shí)點(diǎn),需要的朋友們學(xué)習(xí)下。2019-04-04用python對(duì)excel進(jìn)行操作(讀,寫,修改)
這篇文章主要介紹了用python對(duì)excel進(jìn)行操作(讀,寫,修改),幫助大家更好的利用python處理表格,感興趣的朋友可以了解下2020-12-12PyTorch里面的torch.nn.Parameter()詳解
今天小編就為大家分享一篇PyTorch里面的torch.nn.Parameter()詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python實(shí)現(xiàn)求兩個(gè)數(shù)組交集的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)求兩個(gè)數(shù)組交集的方法,涉及Python數(shù)組遍歷、排序、判斷、追加等相關(guān)操作技巧,需要的朋友可以參考下2019-02-02Python獲取統(tǒng)計(jì)自己的qq群成員信息的方法
這篇文章主要介紹了Python獲取統(tǒng)計(jì)自己的qq群成員信息的方法,本文分步驟給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-11-11