Python影像辨識筆記(七): Pillow - Python圖片處理模組
2 min readMay 19, 2019
安裝
pip install Pillow
開啟圖片
from PIL import Image
pil_im = Image.open(‘empire.jpg’)
顯示圖片尺寸
w, h = pil_im.sizeprint('Original image size: %sx%s' % (w, h))
轉換成灰階
pil_im = Image.open('empire.jpg').convert('L')
批次轉換圖片至.jpg格式
from PIL import Image
import osfor infile in filelist:
outfile = os.path.splitext(infile)[0] + ".jpg"
if infile != outfile:
try:
Image.open(infile).save(outfile)
except IOError:
print("cannot convert", infile)
創建縮圖
pil_im.thumbnail((128,128))
調整尺寸
out = pil_im.resize((128,128))
旋轉
out = pil_im.rotate(45)
模糊效果
from PIL import Image, ImageFilter
im = Image.open('test.jpg')
#使用濾鏡
im2 = im.filter(ImageFilter.BLUR)
im2.save('blur.jpg', 'jpeg')
存檔
out.save('thumbnail.jpg', 'jpeg')