Python implements the method of for resizing images based on the Pillow package

  • 2020-05-12 02:48:26
  • OfStack

This article illustrates how Python can change the size of an image. I will share it with you for your reference as follows:

1. PIL package recommends Pillow.

2. Source code:


#encoding=utf-8
#author: walker
#date: 2014-05-15
#function:  Change the image size 
import os
import os.path
from PIL import Image
'''
filein:  The input image 
fileout:  The output image 
width:  Output picture width 
height: Output image height 
type: Output picture 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)

More about Python related content interested readers to view this site project: Python pictures skills summary, "Python data structure and algorithm tutorial", "Python Socket programming skills summary", "Python function using techniques", "Python string skills summary", "Python introduction and advanced tutorial" and "Python file and directory skills summary"

I hope this article has helped you with your Python programming.


Related articles: