1. 程式人生 > >在python中實現圖片尺寸變換。

在python中實現圖片尺寸變換。

#coding=utf-8
import os
import os.path
from PIL import Image
def ResizeImage(filein, fileout, width, height, type):
  img = Image.open(filein)
  out = img.resize((width, height),Image.ANTIALIAS) #resize image with high-quality
  out.save(fileout, type)
if __name__ == "__main__":
  filein = r'test.jpg'
  fileout = r'testout.jpg'
  width = 28
  height = 28
  type = 'jpeg'#型別可以改
  ResizeImage(filein, fileout, width, height, type)

注意圖片型別有:jpeg格式,不要寫成jpg格式否則會報KeyError:Jpg