验证码

字母验证码
import os
from tkinter import Image


def gen_vcode(request):
    #1- 创建画布
    image = Image.new('RGB',(100,50),(100,20,30))

    #2- 创建画笔
    draw = ImageDraw.Draw(image,'RGB')

    #3- 字体样式
    font_path = os.path.join(BASE_DIR,'App/static/fonts/balabala')
    font = Image.truetype(font_path,30)

    #4- 画
    draw.text((30,10),'M',font=font,fill=(200,200,200))

    #5- 转化成图片
    buff = io.BytesIO()
    image.save(buff,'png')
    return HttpResponse(buff.getvalue(),'image/png')

你可能感兴趣的:(验证码)