免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
圖像處理標準庫pillow

pillow模塊

PIL:Python Imaging Library,已經(jīng)是Python平臺事實上的圖像處理標準庫了。PIL功能非常強大,但API卻非常簡單易用。

由于PIL僅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基礎上創(chuàng)建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又加入了許多新特性,因此,我們可以直接安裝使用Pillow。

安裝pillow

pycharm安裝

命令行安裝

Python中引入

from PIL import Image     #生成一張圖片的第三方模塊from PIL import ImageDraw #在圖片上寫字from PIL import ImageFont #生成字體對象

PIL使用之驗證碼

ps:驗證碼臨時存入內(nèi)存

from io import BytesIO    #內(nèi)存管理器(存臨時驗證碼)
def get_code(request):    # 生成一張新圖片    new_img = Image.new('RGB',(171,34),color=get_random_color())    # 把圖片放到ImageDraw.Draw內(nèi)(畫筆)    draw = ImageDraw.Draw(new_img)    # 構(gòu)造字體對象第一個參數(shù)是字體文件(ttf格式http://www.downcc.com/k/ttfziti/),第二個參數(shù)是字體大小    font = ImageFont.truetype('static/font/simsun.ttf',30)    valid_code = ''    for i in range(5):        num_str = str(random.randint(0,9))        upper_str = chr(random.randint(65,90))        low_str = chr(random.randint(97,122))        random_str = random.choice([num_str,upper_str,low_str])        draw.text((i*28 20,1),random_str,get_random_color(),font=font)        valid_code =random_str    print(valid_code)    # 把驗證碼存到session    request.session['valid_code']=valid_code    # 打開一個內(nèi)存管理器,保存進去    img = BytesIO()    new_img.save(img,'png')    # 從內(nèi)存管理器取出img    data = img.getvalue()    return HttpResponse(data)

前后臺對比

code = request.POST.get('code')if code.upper() == request.session.get('valid_code').upper():    pass

前端點擊更換驗證碼

<img src="/get_code/" class="col-xs-8" style="padding-left: 5px;padding-right: 1px" alt="" height="34" id="id_img"><script>//點擊圖片刷新功能    $("#id_img").click(function () {    $(this)[0].src=$(this)[0].src "?"    });</script>

生成隨機數(shù)顏色

def get_random_color():    '''    生成3個隨機數(shù)顏色    '''    return (random.randint(0,255),random.randint(0,255),random.randint(0,255))
get_random_color()

?

參考該文章:點我

來源:http://www.icode9.com/content-4-186401.html
本站僅提供存儲服務,所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
Pillow
用python編寫驗證碼
“智能投研”還是智能投顧?
Flask后端實踐 連載十 Flask圖形驗證碼生成及驗證
使用 PIL 生成驗證碼圖片
python產(chǎn)生隨機值
更多類似文章 >>
生活服務
分享 收藏 導長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服