Python生成隨機驗證碼

Python生成隨機驗證碼,須要使用PIL模塊.css

安裝:html

pip3 install pillow

 

基本使用

def check_code(request):
	#Django imag標籤src屬性導入圖片的原理
	f=open('static/imgs/1.jpg','rb')    #注意:static/imgs/1.jpg不能寫成 /static/imgs/1.jpg
	data=f.read()
	f.close()
	return HttpResponse(data)

寫入本地並讀取到內存中python

def check_code(request):
	#建立code.png寫入到本地目錄下
	from PIL import Image
	img=Image.new(mode='RGB',size=(120,30),color=(255,255,255)) #默認寫入白板,隨後在白板上寫入字符串等
	f=open('code.png','wb')
	img.save(f,'png')   #png爲圖片後綴
	f.close()
	#將本地圖片code.png讀取到內存並使網頁圖片能顯示出來
	f=open('code.png','rb')
	data=f.read()
	f.close()
	return HttpResponse(data)

以上操做比較麻煩,須要寫入本地又讀取到內存中django

優化:直接在內存中開闢空間,在內存中進行讀寫等操做bootstrap

def check_code(request):	
	from io import BytesIO
	#寫入內存中
	f=BytesIO()
	img=Image.new(mode='RGB',size=(120,30),color=(255,255,255))
	img.save(f,'png')
	#從內存中讀出來
	data=f.getvalue()
	return HttpResponse(data)

 

1. 建立圖片session

from PIL import Image
img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
 
# 在圖片查看器中打開
# img.show() 
 
# 保存在本地
with open('code.png','wb') as f:
    img.save(f,format='png')

2. 建立畫筆,用於在圖片上畫任意內容app

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')

3. 畫點dom

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一個參數:表示座標
# 第二個參數:表示顏色
draw.point([100, 100], fill="red")
draw.point([300, 300], fill=(255, 255, 255))

4. 畫線ide

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一個參數:表示起始座標和結束座標
# 第二個參數:表示顏色
draw.line((100,100,100,300), fill='red')
draw.line((100,100,300,100), fill=(255, 255, 255))

5. 畫圓函數

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一個參數:表示起始座標和結束座標(圓要畫在其中間)
# 第二個參數:表示開始角度
# 第三個參數:表示結束角度
# 第四個參數:表示顏色
draw.arc((100,100,300,300),0,90,fill="red")

6. 寫文本

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一個參數:表示起始座標
# 第二個參數:表示寫入內容
# 第三個參數:表示顏色
draw.text([0,0],'python',"red")

7. 特殊字體文字

img = Image.new(mode='RGB', size=(120, 30), color=(255, 255, 255))
draw = ImageDraw.Draw(img, mode='RGB')
# 第一個參數:表示字體文件路徑
# 第二個參數:表示字體大小
font = ImageFont.truetype("kumo.ttf", 28)
# 第一個參數:表示起始座標
# 第二個參數:表示寫入內容
# 第三個參數:表示顏色
# 第四個參數:表示顏色
draw.text([0, 0], 'python', "red", font=font)

 

圖片驗證碼

import random
 
def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
    code = []
    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode='RGB')
 
    def rndChar():
        """
        生成隨機字母   
        :return:
        """
        return chr(random.randint(65, 90))
 
    def rndColor():
        """
        生成隨機顏色
        :return:
        """
        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))
 
    # 寫文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        char = rndChar()
        code.append(char)
        h = random.randint(0, 4)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())
 
    # 寫干擾點
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
 
    # 寫干擾圓圈
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())
 
    # 畫干擾線
    for i in range(5):
        x1 = random.randint(0, width)
        y1 = random.randint(0, height)
        x2 = random.randint(0, width)
        y2 = random.randint(0, height)
 
        draw.line((x1, y1, x2, y2), fill=rndColor())
 
    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE)
    return img,''.join(code)
 
 
if __name__ == '__main__':
    # 1. 直接打開
    # img,code = check_code()
    # img.show()
 
    # 2. 寫入文件
    # img,code = check_code()
    # with open('code.png','wb') as f:
    #     img.save(f,format='png')
 
    # 3. 寫入內存(Python3)
    # from io import BytesIO
    # stream = BytesIO()
    # img.save(stream, 'png')
    # stream.getvalue()
 
    # 4. 寫入內存(Python2)
    # import StringIO
    # stream = StringIO.StringIO()
    # img.save(stream, 'png')
    # stream.getvalue()

  

 

總結:看成模板使用

方法一:

#登陸頁面
def login(request):

    if request.method=='POST':
        input_username=request.POST.get('user')
        input_pwd=request.POST.get('pwd')
        input_code=request.POST.get('code')
        session_code=request.session.get('code')
        print(input_code,session_code)

        if models.UserInfo.objects.filter(username=input_username,password=input_pwd).first():
            if input_code.upper() == session_code.upper():
                return render(request,'information.html')
    return render(request,'login.html')





#生成隨機驗證碼
from PIL import Image
from io import BytesIO
from PIL import ImageDraw,ImageFont
def check_code(request):

    # #Django imag標籤src屬性導入圖片的原理
    # f=open('static/imgs/1.jpg','rb')    #注意:static/imgs/1.jpg不能寫成 /static/imgs/1.jpg
    # data=f.read()
    # f.close()
    # return HttpResponse(data)
    #建立code.png寫入到本地BBS目錄下
    # from PIL import Image
    # img=Image.new(mode='RGB',size=(120,30),color=(255,255,255)) #默認寫入白板,隨後在白板上寫入字符串等
    # f=open('code.png','wb')
    # img.save(f,'png')   #png爲圖片後綴
    # f.close()
    # #將本地圖片code.png讀取到內存並使網頁圖片能顯示出來
    # f=open('code.png','rb')
    # data=f.read()
    # f.close()
    # return HttpResponse(data)
    #以上操做比較麻煩,須要寫入本地又讀取到內存中
    #如下操做爲在內存中開闢空間,在內存中進行讀寫等操做
    # from io import BytesIO
    # #寫入內存中
    # f=BytesIO()
    # img=Image.new(mode='RGB',size=(120,30),color=(255,255,255))
    # img.save(f,'png')
    # #從內存中讀出來
    # data=f.getvalue()
    # return HttpResponse(data)

    # from io import BytesIO
    # from PIL import ImageDraw,ImageFont
    f=BytesIO()
    img=Image.new(mode='RGB',size=(120,30),color=(255,255,255))
    draw=ImageDraw.Draw(img,mode='RGB')
    # draw.point([100, 25], fill="red")
    # draw.point([30, 20], fill=(187, 255, 255))
    # draw.line((10, 10, 20, 30), fill='red')
    # draw.line((100, 10, 30, 20), fill=(180, 255, 255))
    # draw.arc((30, 25, 80, 10), 0, 360, fill="red")
    # font = ImageFont.truetype("kumo.ttf", 28)
    # draw.text([0, 0], 'python', "red",font=font)
    import random
    # char_list=[]
    # for i in range(5):
    #     char=chr(random.randint(65,109))
    #     char_list.append(char)
    # ''.join(char_list)
    # #列表生成式,可是字母隨機了,字體顏色不隨機,因此不用列表生成式
    # v=''.join([ chr(random.randint(65,109)) for i in range(5)])
    # for i in range(5):
    #     char=chr(random.randint(65,109))
    #     font=ImageFont.truetype("kumo.ttf", 28)
    #     draw.text([i*24,0],char,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),font=font)
    #寫入的字符串須要本身知道內容,方便表單的校驗
    # char_list = []
    # for i in range(5):
    #     char=chr(random.randint(65,109))
    #     char_list.append(char)
    #     font=ImageFont.truetype("kumo.ttf", 28)
    #     draw.text([i*24,0],char,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),font=font)
    # code=''.join(char_list)
    #但又面臨的問題是POST請求時提交數據,獲取不到,因此須要寫入session裏,並進入login函數中增長POST請求
    # char_list = []
    # for i in range(5):
    #     char = chr(random.randint(65, 109))
    #     char_list.append(char)
    #     font = ImageFont.truetype("kumo.ttf", 28)
    #     draw.text([i * 24, 0], char, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
    #               font=font)
    # img.save(f,'png')
    # data=f.getvalue()
    # code = ''.join(char_list)
    # request.session['code'] = code
    # return HttpResponse(data)

    #以上驗證碼代碼太長,將內容封裝到BBS目錄下utils目錄中
    from utils.random_check_code import random_check_code
    img,code=random_check_code()
    stream=BytesIO()
    img.save(stream,'png')
    request.session['code']=code
    return HttpResponse(stream.getvalue())
views
urlpatterns = [
    url(r'^login/', views.login),
    url(r'^check_code/', views.check_code),
]
urls
import random
from PIL import ImageDraw,ImageFont,Image,ImageFilter


def random_check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
    code = []
    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode='RGB')

    def rndChar():
        """
        生成隨機字母
        :return:
        """
        return chr(random.randint(65, 90))

    def rndColor():
        """
        生成隨機顏色
        :return:
        """
        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))

    # 寫文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        char = rndChar()
        code.append(char)
        h = random.randint(0, 4)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())

    # 寫干擾點
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

    # 寫干擾圓圈
    for i in range(40):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())

    # 畫干擾線
    for i in range(5):
        x1 = random.randint(0, width)
        y1 = random.randint(0, height)
        x2 = random.randint(0, width)
        y2 = random.randint(0, height)
        draw.line((x1, y1, x2, y2), fill=rndColor())

    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) #加濾鏡,能夠增長顏色的不一樣
    return img, ''.join(code)
utils/random_check_code.py
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>$Title$</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.5-dist/css/bootstrap.css">
    <style>
        .login{
            margin: 0 auto;
            padding: 20px;
            margin-top: 150px;
            margin-left: 350px;
        }
    </style>
</head>

<body>

<div class="login">
    <form class="form-horizontal" method="POST">
        {% csrf_token %}

    <div class="form-group">
        <label class="col-sm-2 control-label">用戶名</label>
        <div class="col-sm-4">
            <input name="user" class="form-control" placeholder="用戶名">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">密碼</label>
        <div class="col-sm-4">
            <input type="password"  name="pwd" class="form-control" placeholder="密碼">
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">驗證碼</label>
        <div class="col-sm-2">
            <input class="form-control" placeholder="驗證碼" name="code">
        </div>

        <div class="col-sm-2">
            <img src="/check_code/" alt="" style="width: 120px;height: 30px">
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">登陸</button>
        </div>
    </div>

</form>
</div>

</body>
</html>
login.html

方法二:

使用Form表單:

#生成隨機驗證碼
from PIL import Image
from io import BytesIO
from PIL import ImageDraw,ImageFont
def check_code(request):

    # #Django imag標籤src屬性導入圖片的原理
    # f=open('static/imgs/1.jpg','rb')    #注意:static/imgs/1.jpg不能寫成 /static/imgs/1.jpg
    # data=f.read()
    # f.close()
    # return HttpResponse(data)
    #建立code.png寫入到本地BBS目錄下
    # from PIL import Image
    # img=Image.new(mode='RGB',size=(120,30),color=(255,255,255)) #默認寫入白板,隨後在白板上寫入字符串等
    # f=open('code.png','wb')
    # img.save(f,'png')   #png爲圖片後綴
    # f.close()
    # #將本地圖片code.png讀取到內存並使網頁圖片能顯示出來
    # f=open('code.png','rb')
    # data=f.read()
    # f.close()
    # return HttpResponse(data)
    #以上操做比較麻煩,須要寫入本地又讀取到內存中
    #如下操做爲在內存中開闢空間,在內存中進行讀寫等操做
    # from io import BytesIO
    # #寫入內存中
    # f=BytesIO()
    # img=Image.new(mode='RGB',size=(120,30),color=(255,255,255))
    # img.save(f,'png')
    # #從內存中讀出來
    # data=f.getvalue()
    # return HttpResponse(data)

    # from io import BytesIO
    # from PIL import ImageDraw,ImageFont
    f=BytesIO()
    img=Image.new(mode='RGB',size=(120,30),color=(255,255,255))
    draw=ImageDraw.Draw(img,mode='RGB')
    # draw.point([100, 25], fill="red")
    # draw.point([30, 20], fill=(187, 255, 255))
    # draw.line((10, 10, 20, 30), fill='red')
    # draw.line((100, 10, 30, 20), fill=(180, 255, 255))
    # draw.arc((30, 25, 80, 10), 0, 360, fill="red")
    # font = ImageFont.truetype("kumo.ttf", 28)
    # draw.text([0, 0], 'python', "red",font=font)
    import random
    # char_list=[]
    # for i in range(5):
    #     char=chr(random.randint(65,109))
    #     char_list.append(char)
    # ''.join(char_list)
    # #列表生成式,可是字母隨機了,字體顏色不隨機,因此不用列表生成式
    # v=''.join([ chr(random.randint(65,109)) for i in range(5)])
    # for i in range(5):
    #     char=chr(random.randint(65,109))
    #     font=ImageFont.truetype("kumo.ttf", 28)
    #     draw.text([i*24,0],char,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),font=font)
    #寫入的字符串須要本身知道內容,方便表單的校驗
    # char_list = []
    # for i in range(5):
    #     char=chr(random.randint(65,109))
    #     char_list.append(char)
    #     font=ImageFont.truetype("kumo.ttf", 28)
    #     draw.text([i*24,0],char,(random.randint(0,255),random.randint(0,255),random.randint(0,255)),font=font)
    # code=''.join(char_list)
    #但又面臨的問題是POST請求時提交數據,獲取不到,因此須要寫入session裏,並進入login函數中增長POST請求
    # char_list = []
    # for i in range(5):
    #     char = chr(random.randint(65, 109))
    #     char_list.append(char)
    #     font = ImageFont.truetype("kumo.ttf", 28)
    #     draw.text([i * 24, 0], char, (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)),
    #               font=font)
    # img.save(f,'png')
    # data=f.getvalue()
    # code = ''.join(char_list)
    # request.session['code'] = code
    # return HttpResponse(data)

    #以上驗證碼代碼太長,將內容封裝到BBS目錄下utils目錄中
    from utils.random_check_code import random_check_code
    img,code=random_check_code()
    stream=BytesIO()
    img.save(stream,'png')
    request.session['code']=code
    return HttpResponse(stream.getvalue())






#登陸頁面,使用Form表單
#生成Form表單
from django.forms import Form
from django.forms import widgets
from django.forms import fields

class LoginForm(Form):
    user = fields.CharField(
        widget=widgets.TextInput(attrs={'class':"form-control",'placeholder':'用戶名'}),
        label="用戶名",
        required=True,
        max_length=8,
        min_length=2,
        error_messages={
            'required':'用戶名不能爲空',
            'min_length':'用戶名不得少於2個字符',
            'max_length':'用戶名不得多於8個'
        }
    )

    pwd = fields.CharField(
        widget=widgets.TextInput(attrs={'class':"form-control",'placeholder':'密碼','type':'password'}),
        label="密碼",
        required=True,
        max_length=10,
        min_length=2,
        error_messages={
            'required': '密碼不能爲空',
            'min_length': '密碼不得少於2個字符',
            'max_length': '密碼不得多於10個'
        }
    )

    code=fields.CharField(
        widget=widgets.TextInput(attrs={'class':"form-control",'placeholder':'驗證碼'}),
        label="驗證碼",
        required=True,
        error_messages={
            'required':'驗證碼不能爲空'
        }
    )





def login_form(request):
    if request.method=='GET':
        obj = LoginForm()
        return render(request, 'login_form.html', {'obj': obj})
    else:
        obj=LoginForm(request.POST)
        if obj.is_valid():
            input_user=obj.cleaned_data['user']
            input_pwd=obj.cleaned_data['pwd']
            input_code =obj.cleaned_data['code']
            session_code=request.session.get('code')

            if models.UserInfo.objects.filter(username=input_user,password=input_pwd).first():
                if input_code.upper()==session_code.upper():
                    return render(request,'information.html')
                else:
                    return render(request, 'login_form.html', {'obj': obj, 'msg': '驗證碼錯誤'})
            return render(request, 'login_form.html',{'obj':obj,'msg':'用戶名或密碼錯誤'})
        else:
            v=obj.errors
            print(v)
            return render(request,'login_form.html',{'obj':obj})
views
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>$Title$</title>
    <link rel="stylesheet" href="/static/bootstrap-3.3.5-dist/css/bootstrap.css">
    <style>
        .login{
            margin: 0 auto;
            padding: 20px;
            margin-top: 150px;
            margin-left: 350px;
        }


    </style>
</head>

<body>

<div class="login">
    <form class="form-horizontal" method="POST" novalidate>
    {% csrf_token %}
    <div class="form-group">
        <label class="col-sm-2 control-label">{{ obj.user.label }}</label>
        <div class="col-sm-4">
            {{ obj.user }}{{ obj.errors.user.0 }}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">密碼</label>
        <div class="col-sm-4">
            {{ obj.pwd }}{{ obj.errors.pwd.0 }}
        </div>
    </div>

    <div class="form-group">
        <label class="col-sm-2 control-label">驗證碼</label>
        <div class="col-sm-2">
            {{ obj.code }}{{ obj.errors.code.0 }}{{ msg }}
        </div>

        <div class="col-sm-2">
            <img src="/check_code/" alt="" style="width: 120px;height: 30px">
        </div>
    </div>

    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">登陸</button>
        </div>
    </div>

</form>
</div>

</body>
</html>
login_form
urlpatterns = [

    url(r'^check_code/', views.check_code),

    url(r'^login_form/', views.login_form),

]
urls
import random
from PIL import ImageDraw,ImageFont,Image,ImageFilter


def random_check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
    code = []
    img = Image.new(mode='RGB', size=(width, height), color=(255, 255, 255))
    draw = ImageDraw.Draw(img, mode='RGB')

    def rndChar():
        """
        生成隨機字母
        :return:
        """
        return chr(random.randint(65, 90))

    def rndColor():
        """
        生成隨機顏色
        :return:
        """
        return (random.randint(0, 255), random.randint(10, 255), random.randint(64, 255))

    # 寫文字
    font = ImageFont.truetype(font_file, font_size)
    for i in range(char_length):
        char = rndChar()
        code.append(char)
        h = random.randint(0, 4)
        draw.text([i * width / char_length, h], char, font=font, fill=rndColor())

    # 寫干擾點
    for i in range(10):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())

    # 寫干擾圓圈
    for i in range(10):
        draw.point([random.randint(0, width), random.randint(0, height)], fill=rndColor())
        x = random.randint(0, width)
        y = random.randint(0, height)
        draw.arc((x, y, x + 4, y + 4), 0, 90, fill=rndColor())

    # 畫干擾線
    for i in range(2):
        x1 = random.randint(0, width)
        y1 = random.randint(0, height)
        x2 = random.randint(0, width)
        y2 = random.randint(0, height)
        draw.line((x1, y1, x2, y2), fill=rndColor())

    img = img.filter(ImageFilter.EDGE_ENHANCE_MORE) #加濾鏡,能夠增長顏色的不一樣
    return img, ''.join(code)
utils/random_check_code.py
相關文章
相關標籤/搜索