1. 程式人生 > >python實現圖片驗證碼

python實現圖片驗證碼

stat 模塊 app req param div pytho python ram

1 驗證基礎知識
1.1 Python生成隨機驗證碼,需要使用PIL模塊.

# 安裝
pip3 install pillow

1.2 創建圖片

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

# 保存圖片到本地
with open("code.png",wb) as f:
img.save(f,format="png")

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

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.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.5 畫線

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.6 畫圓

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.7 寫文本

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.8 設置特殊字體

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)

python實現圖片驗證碼