top of page

How to Resize Images in Python Using Pillow

  • Writer: Anvita Shrivastava
    Anvita Shrivastava
  • 16 hours ago
  • 3 min read

Image resizing is one of the frequently performed operations in various Python-based projects that use web development, machine learning, computer vision, remote sensing, GIS, photography, or image processing. Resizing an image might be needed to decrease its file size, prepare images for deep learning, make thumbnails, standardize the sizes of the images, or to optimize them for the website.


Pillow is one of the most popular Python packages for image manipulation. It offers an easy way to open, resize, crop, rotate, convert, and save image files. In this tutorial, we will show how to perform image resizing in Python using the Pillow library.


Resize Images in Python Using Pillow
Resize Images in Python Using Pillow

What Is Pillow in Python?


Pillow is the PIL fork for Python. It supports common image formats, including JPEG, PNG, TIFF, BMP, GIF, and WebP.


Before resizing the images, we will need to install Pillow with pip:


pip install Pillow


We can then import the Image module as follows:


from PIL import Image


Resize an Image to a Specific Width and Height


Resizing an image is simple with the use of the resize() method.


from PIL import Image


image = Image.open("input.jpg")


resized_image = image.resize((800, 600))


resized_image.save("output.jpg")


Here, we have resized the image to 800 x 600 pixels.


The syntax is as follows:


image.resize((width, height))


But this may distort the image since both dimensions have been defined explicitly.


Resizing an Image Keeping the Aspect Ratio Same


To ensure no distortion happens, calculate the new sizes using the scaling factor.


from PIL import Image


image = Image.open("input.jpg")


width, height = image.size


new_width = 800

new_height = int(height * (new_width / width))


resized_image = image.resize((new_width, new_height))


resized_image.save("resized.jpg")


In this way, you can automatically calculate the new height and keep the aspect ratio the same.


Resizing an Image with the use of thumbnail() Function


Another feature available in Pillow is the thumbnail() function that resizes the images with automatic adjustment of the aspect ratio.


from PIL import Image


image = Image.open("input.jpg")


image.thumbnail((800, 600))


image.save("thumbnail.jpg")


This way, the image fits into an 800 x 600 bounding box.


Unlike the resize() function, the thumbnail() function automatically adjusts the size of the image and does not enlarge the image beyond its original size.


Efficient Resizing of Large Images


When working with big data sets, satellite pictures, aerial photography, or high-definition photos, resizing images might take up quite a lot of memory.


The thumbnail() function could be helpful in this situation since it resizes images in place:


from PIL import Image


image = Image.open("large_image.jpg")


image. thumbnail(

(1920, 1080),

Image.Resampling.LANCZOS

)


image.save("optimized_image.jpg")


If you work with big raster data sets or geospatial imagery, you may want to consider libraries like GDAL or Rasterio that are specifically designed for them.


Image Quality and File Size


Image resizing and compression are two different things. An image that is small in size will have fewer pixels in it, while image compression defines how pixels are represented.


For JPEG files, the following can be specified when saving an image:


resized_image.save(

"output.jpg",

quality=90,

optimize=True

)


The quality value is usually a trade-off between the quality of the image and the file size.


Resize Images in Python Without Distortion


It is usually better to maintain the original aspect ratio in almost all cases. The following method can be used:


from PIL import Image


image = Image.open("input.jpg")


image. thumbnail(

(1024, 1024),

Image.Resampling.LANCZOS

)


image.save("output.jpg")


Thus, the image would resize to fit into the given dimensions.


Common Use Cases for Image Resizing in Python


Python image resizing using Pillow is commonly done for:


  • Web image optimization

  • Image processing on social media

  • Generating thumbnails

  • Data preprocessing for machine learning applications

  • Computer vision applications

  • Standardization of datasets

  • Drone imaging and aerial imaging applications

  • Photographic applications

  • Document image processing

  • Batch image processing


Images in machine learning and computer vision applications need to be preprocessed into standardized image sizes like 224 × 224, 512 × 512, or 640 × 640 pixels.


Python provides a simple and versatile tool that allows you to resize images using the Pillow library. The resize() function will come in handy when you need to resize images to a specified size, but the thumbnail() function should be used when the aspect ratio needs to be preserved.


To achieve the highest quality of the resized images, you can use up-to-date resampling filters like Image. Resampling. LANCZOS.Resampling.LANCZOS. Pillow can help you with batch processing of images, as well as with resizing images proportionally, optimizing them for web applications and machine learning projects.


No matter whether you deal with one photo or a thousand, there is a convenient solution to your problem using Python and Pillow.


To learn more about Pillow and its geospatial capabilities, click here.


For more information or any questions regarding the LizardTech suite of products, please don't hesitate to contact us at:



USA (HQ): (720) 702–4849


(A GeoWGS84 Corp Company)



Comments


bottom of page