以前都是用PIL處理圖像,但是現(xiàn)在已經(jīng)太久沒更新,
pip install PIL會提示找不到
現(xiàn)在已經(jīng)用Pillow代替了PIL,
pip install Pillow
使用上依然是PIL的命名空間
使用方法,以下是縮放圖片舉例
from PIL import Image
im = Image.open(file)
size = (40,40)
im.thumdnail(size) #縮放圖片
im.save(path)
from PIL import Image
import io
#png/jpg轉(zhuǎn)webp
def img2webp(image_bytes):
data_stream = io.BytesIO(image_bytes)
img = Image.open(data_stream)
#輸出字節(jié)流
data_stream = io.BytesIO()
img.save(data_stream, format='WEBP',quality=85)#質(zhì)量為80-85效果最好
out_bytes = data_stream.getvalue()
return out_bytes