Home >Backend Development >Python Tutorial >Python implements the method of changing the image size (based on the Pillow package)

Python implements the method of changing the image size (based on the Pillow package)

高洛峰
高洛峰Original
2017-01-14 13:22:522613browse

The example of this article describes the method of changing the image size in Python. Share it with everyone for your reference, the details are as follows:

1. PIL package recommends Pillow.

2. Source code:

#encoding=utf-8
#author: walker
#date: 2014-05-15
#function: 更改图片尺寸大小
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)

I hope this article will be helpful to everyone in Python programming.

For more Python methods to change the image size (based on the Pillow package), please pay attention to the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn