django 處理上傳圖片生成縮略圖首先要注意form標(biāo)簽上必須有enctype="multipart/form-data"屬性,另外要裝好PIL庫, 然后就很簡單了,如下是實例代碼:
upload.html
<div id="uploader"> <form id="upload" enctype="multipart/form-data" action="/ajax/upload/" method="post"> <input id="file" name="file" type="file"> <input type="submit" value="Upload"> </form> </div>
view.py
# -*- coding: utf-8 -*- from django.http import HttpResponse import Image def upload(request): reqfile = request.FILES['file'] image = Image.open(reqfile) image.thumbnail((128,128),Image.ANTIALIAS) image.save("/home/lhb/1.jpeg","jpeg") return HttpResponse("success.") 下面介紹下生成縮略圖質(zhì)量差的解決辦法。
使用python的PIL庫的thumbnail方法生成縮略圖的質(zhì)量很差,需要使用resize方法來生成縮略圖,并制定縮略圖的質(zhì)量,如下代碼:
image = image.resize((x, y), Image.ANTIALIAS)quality_val = 90image.save(filename, 'JPEG', quality=quality_val)
總結(jié)
以上就是本文關(guān)于django上傳圖片并生成縮略圖方法示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:
Python內(nèi)置模塊turtle繪圖詳解
Python實戰(zhàn)小程序利用matplotlib模塊畫圖代碼分享
Python科學(xué)畫圖代碼分享
如有不足之處,歡迎留言指出。感謝朋友們對本站的支持!
新聞熱點
疑難解答