自訂的驗證碼

來源:互聯網
上載者:User

標籤:image   random   bytesio   close   raw   bsp   text   圖片   from   

from PIL import Imagefrom PIL import ImageDraw, ImageFontimport randomdef get_valid_img(request):    def get_random_color():        return (random.randint(0, 255),                random.randint(0, 255),                random.randint(0, 255))    image = Image.new("RGB", (250, 40), get_random_color())    # 產生五個隨機字元    draw = ImageDraw.Draw(image)    font = ImageFont.truetype("static/blog/font/kumo.ttf", size=32)    temp = []    for i in range(5):        random_num = str(random.randint(0, 9))        random_low_alpha = chr(random.randint(97, 122)) #得到A~Z        random_upper_alpha = chr(random.randint(65, 90)) #得到a~z        random_char = random.choice([random_num, random_low_alpha, random_upper_alpha])        draw.text((24+i*36, 0), random_char, get_random_color(), font=font)        # 儲存隨機字元        temp.append(random_char)    # 噪點噪線    # width=250    # height=40    # for i in range(100):    #     x1=random.randint(0,width)    #     x2=random.randint(0,width)    #     y1=random.randint(0,height)    #     y2=random.randint(0,height)    #     draw.line((x1,y1,x2,y2),fill=get_random_color())    #    # for i in range(400):    #     draw.point([random.randint(0, width), random.randint(0, height)], fill=get_random_color())    #     x = random.randint(0, width)    #     y = random.randint(0, height)    #     draw.arc((x, y, x + 4, y + 4), 0, 90, fill=get_random_color())    # 在記憶體產生圖片    from io import BytesIO    f = BytesIO()    image.save(f, "png")    data = f.getvalue()    f.close()    valid_str = "".join(temp)    print("valid_str", valid_str)    # 設定session    request.session["valid_str"] = valid_str    return data

 

自訂的驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.