Django-實現圖片驗證碼


Django實現圖片驗證碼

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

pip3 install pillow

1、基本使用

1. 1 創建圖片

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')

1、2 創建畫筆

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")

1、3 畫點

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一個參數:表示坐標
# 第二個參數:表示顏色
draw.point([100,20], fill="red")
draw.point([50,10], fill=(255, 135, 255))
with open("code.png",'wb') as f:
img.save(f,format="png")

1、4 畫線

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一個參數:表示起始坐標和結束坐標
# 第二個參數:表示顏色
draw.line((10, 10, 10, 30), fill="red")
draw.line((10, 10, 30, 10), fill=(255, 135, 255))
with open("code.png",'wb') as f:
img.save(f,format="png")

1、5 畫圓

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(150, 150), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一個參數:表示起始坐標和結束坐標(圓要畫在中間)
# 第二個參數:表示開始角度
# 第三個參數:表示結束角度
# 第四個參數:表示顏色
draw.arc((50, 50, 100, 100), 0, 360, fill="red")
with open("code.png",'wb') as f:
img.save(f,format="png")

1、6 寫文本

from PIL import Image
from PIL import ImageDraw
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一個參數:表示起始坐標
# 第二個參數:表示寫入內容
# 第三個參數:表示顏色
draw.text([20, 10], "python", fill="red")
with open("code.png",'wb') as f:
img.save(f,format="png")

1、7設置特殊字體

from PIL import Image, ImageDraw, ImageFont
img = Image.new(mode="RGB", size=(120, 30), color=(255,255,255))
draw = ImageDraw.Draw(img, mode="RGB")
# 第一個參數:表示字體路徑
# 第二個參數:表示字體大小
font = ImageFont.truetype("kumo.ttf", 25)
# 第一個參數:表示起始坐標
# 第二個參數:表示寫入內容
# 第三個參數:表示顏色
# 第四個參數:表示字體
draw.text([30, 0], "python", fill="red", font=font)
with open("code.png",'wb') as f:
img.save(f,format="png")

2

2.1 應用方法

import random
from io import BytesIO
from PIL import Image, ImageDraw, ImageFont

def check_code(width=120, height=30, char_length=5, font_file='kumo.ttf', font_size=28):
    f = BytesIO()
    img = Image.new(mode='RGB', size=(width, height),
                    color=(random.randint(50, 250), random.randint(50, 255), random.randint(50, 255)))
    draw = ImageDraw.Draw(img, mode='RGB')

    char_list = []
    # 畫字
    for i in range(char_length):
        char = random.choice([chr(random.randint(65, 90)), str(random.randint(1, 9)), chr(random.randint(97, 122)), ])
        font = ImageFont.truetype("blog/static/fonts/"+font_file, font_size)
        draw.text([i * 24, 0], char, (random.randint(200, 255), random.randint(0, 50), random.randint(0, 50)),
                  font=font)
        char_list.append(char)

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

    # 寫干擾點
    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.save(f, "png")
    data = f.getvalue()
    s_code = ''.join(char_list)
    return data,s_code

2.2 調用方法

def get_valid_img(request):
    '''
    登錄和注冊的驗證碼
    :param request:
    :return:
    '''
    data,s_code = check_code()
    request.session["valid_code"] = s_code
    return HttpResponse(data)

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM