# 生成隨機驗證碼
def identificode(request):
? ? aaa = ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','j','k','m','n','o','p','q','r','s','t','u','v','w','x','y','z','A','B','C','D','E','F','G','H','J','K','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
? ? bg= (0 , 255 ,200)
? ? backcolor= (random.randint(32, 128), random.randint(32, 128), random.randint(32, 128))
? ? w = 60 * 4;
? ? h = 60
? ? # 創(chuàng)建一張圖片剧防,指定圖片mode,長寬
? ? image = Image.new('RGB', (w, h), (255,182,193))
? ? # 設(shè)置字體類型及大小
? ? font = ImageFont.truetype(font='arial.ttf', size=36)
? ? # 創(chuàng)建Draw對象
? ? draw = ImageDraw.Draw(image)
? ? # 將隨機生成的chr,draw如image
? ? global xx;
? ? xx = "";
? ? # 遍歷給圖片的每個像素點著色
? ? for x in range(w):
? ? ? ? for y in range(h):
? ? ? ? ? ? draw.point((x, y),fill=bg)
? ? for t in range(4):
? ? ? ? a=aaa[random.randint(0 , 57)];
? ? ? ? xx=xx+a;
? ? ? ? draw.text((60 * t + 10, 10), a, font=font,fill=backcolor)
? ? xx=str.lower(xx);#大寫字母轉(zhuǎn)小寫
? ? image = image.filter(ImageFilter.BLUR)
? ? image.save('./shopApp/static/myfile/code.jpg', 'jpeg')
? ? imgDic = {"imgPath":"static/myfile/code.jpg"}
? ? return HttpResponse(json.dumps(imgDic) , content_type = "application/json")