python 利用PIL库进行更改图片大小的操作

python 是可以利用PIL库进行更改图片大小的操作的,当然一般情况下是不需要的,但是在一些特殊的利用场合,是需要改变图片的灰度或是大小等的操作的,其实用python更改图片的大小还是蛮简单的,只需要几行代码,有一点可能刚入门的小伙伴们可能不知道PIL库,PIL是一个库的简写,他的真名叫做pillow,因此,需要pip install pillow 用anaconda的话是conda install pillow千万不要pip/conda install PIL咯,下面贴出代码,希望对一些小伙伴们能有帮助

#encoding=utf-8
import os
import os.path
from PIL import Image
'''
filein: 输入图片
fileout: 输出图片
width: 输出图片宽度
height:输出图片高度
type:输出图片类型(png, gif, jpeg...)
'''
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'image\test.png'
  fileout = r'image\testout.png'
  width = 60
  height = 85
  type = 'png'
  ResizeImage(filein, fileout, width, height, type)

  

posted @ 2018-07-22 22:12  雪地里的Alan  阅读(1037)  评论(0编辑  收藏  举报