- PIL Home
- Python Image Library Intro
- Python Image Library Setup
- Python Image Library Basics
- Python Image Library Hierarchy
- Python Image Library Modules
- Python ImageColor Module
- Python ImageDraw Module
- Python ImageStat Module
- Python ImageTK Module
- Python ImageFile Module
- Python ImageFilter Module
- Python ImageGrab Module
- Python ImageMath Module
- Python - Loading Image
- Python - Rotating Image
- Python - Blurring Image
- Python - Converting Image
- Python - Cropping Image
- Python - Image Thumbnails
- Python - Text on Images
- Python - Multiple Image Copies
- Python - Saving Image As PDF
- Python - Saving Screenshot
- Python - Resizing Image
- Python - Adding Watermark
- Python Image Library Resources
- Python - Quick Guide
- Python - Useful Resources
- Python - Discussion
Python - Resizing Image
Description
Resizing an image is the process to change the size of the image without changing the number of pixels in the image.Parameters
from PIL import Image
im2 = im1.resize((width, height), Image.NEAREST) # use nearest neighbour
im2 = im1.resize((width, height), Image.NEAREST) # use nearest neighbour
Resize method returns a resized copy of this image.
1st argument : param size: The requested size in pixels, as a 2-tuple(width, height).
2nd argument : An optional resampling filter. This can beone of
PIL.Image.NEAREST` (use nearest neighbour),
PIL.Image.BILINEAR` (linear interpolation),
PIL.Image.BICUBIC` (cubic spline interpolation), or
PIL.Image.LANCZOS` (a high-quality downsampling filter).
If omitted, or if the image has mode "1" or "P",
Return value
Below program takes the single file as input and returns a resized copy of this image.Example
The following example shows how to resize the single image into 4 different ways.
from PIL import Image
# open an image file (.bmp,.jpg,.png,.gif) you have in the working folder
imageFile = "flower3.jpg"
im1 = Image.open(imageFile)
# adjust width and height to your needs
width = 500
height = 420
# use one of these filter options to resize the image
im2 = im1.resize((width, height), Image.NEAREST) # use nearest neighbour
im3 = im1.resize((width, height), Image.BILINEAR) # linear interpolation in a 2x2 environment
im4 = im1.resize((width, height), Image.BICUBIC) # cubic spline interpolation in a 4x4 environment
im5 = im1.resize((width, height), Image.ANTIALIAS) # best down-sizing filter
ext = ".jpg"
im2.save("NEAREST" + ext)
im3.save("BILINEAR" + ext)
im4.save("BICUBIC" + ext)
im5.save("ANTIALIAS" + ext)
# open an image file (.bmp,.jpg,.png,.gif) you have in the working folder
imageFile = "flower3.jpg"
im1 = Image.open(imageFile)
# adjust width and height to your needs
width = 500
height = 420
# use one of these filter options to resize the image
im2 = im1.resize((width, height), Image.NEAREST) # use nearest neighbour
im3 = im1.resize((width, height), Image.BILINEAR) # linear interpolation in a 2x2 environment
im4 = im1.resize((width, height), Image.BICUBIC) # cubic spline interpolation in a 4x4 environment
im5 = im1.resize((width, height), Image.ANTIALIAS) # best down-sizing filter
ext = ".jpg"
im2.save("NEAREST" + ext)
im3.save("BILINEAR" + ext)
im4.save("BICUBIC" + ext)
im5.save("ANTIALIAS" + ext)
Advertisements