Python:Pillow .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 thesizeparameter, whileresample,box, andreducing_gapare 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
- Looking for an introduction to the theory behind programming? Master Python while learning data structures, algorithms, and more!
- Includes 6 Courses
- With Professional Certification
- Beginner Friendly.75 hours
- Learn to clean, analyze, and visualize data with Python and SQL.
- Includes 15 Courses
- With Certificate
- Beginner Friendly.55 hours