.resize()
Published May 27, 2024
Contribute to Docs
In Pillow, the .resize()
method is used to change the size of an image. Pillow is a widely used Python library for image processing and manipulation, supporting formats such as JPEG, PNG, PPM, TIFF, GIF, and BMP It can be easily installed on a local PC using pip.
Syntax
Image.resize(size, resample=None, box=None, reducing_gap=None)
size
: A 2-tuple specifying the new size as(width, height)
.resample
: Specifies the resampling filter to be used when resizing.box
: A 4-tuple defining the region of the image to resize.reducing_gap
: A float to optimize downscaling by resizing in multiple steps.
Note: The
.resize()
method requires thesize
parameter, whileresample
,box
, andreducing_gap
are optional parameters for additional control over the resizing process.
Example
The following example demonstrates the use of the .resize()
method to change the size of the image:
from PIL import Image# Original image is 2000x2000 pixels.img = Image.open('pillow-resize-earth.png')# showccasing the original imageimg.show()img_resized = img.resize((500, 500))print(img_resized.height, img_resized.width)# showcasing the resized imageimg_resized.show()
The code snippet will display the new dimensions of the image and also showcase the image in the output as follows:
500 500
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:Pillow on Codecademy
- Career path
Computer Science
Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!Includes 6 CoursesWith Professional CertificationBeginner Friendly75 hours - Skill path
Data Science Foundations
Learn to clean, analyze, and visualize data with Python and SQL.Includes 15 CoursesWith CertificateBeginner Friendly55 hours - Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours