.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.