How to Resize Image in Python

  1. Method 1: Resizing Images Using Pillow
  2. Method 2: Resizing Images Using OpenCV
  3. Method 3: Resizing Images with Aspect Ratio
  4. Conclusion
  5. FAQ
How to Resize Image in Python

Resizing images in Python can be a simple yet powerful task. Whether you’re looking to optimize images for web use, adjust them for a specific display size, or simply want to edit photos for personal projects, Python provides several libraries that make this process seamless.

In this tutorial, we will explore different methods to resize images using popular libraries like Pillow and OpenCV. By the end, you will have a solid understanding of how to manipulate image dimensions with Python, empowering you to enhance your projects with ease. So, let’s dive in and learn how to resize images effectively!

Method 1: Resizing Images Using Pillow

Pillow is a widely-used Python Imaging Library that simplifies image processing tasks, including resizing. To get started, you first need to install Pillow if you haven’t already. You can do this using pip:

pip install Pillow

Once you have Pillow installed, you can use the following code to resize an image:

from PIL import Image

# Open an image file
img = Image.open("example.jpg")

# Resize the image
img_resized = img.resize((800, 600))

# Save the resized image
img_resized.save("example_resized.jpg")

The code above begins by importing the Image class from the Pillow library. It then opens an existing image file called “example.jpg”. The resize method is called on the image object, where you specify the new dimensions as a tuple (width, height). In this case, the image is resized to 800x600 pixels. Finally, the resized image is saved as “example_resized.jpg”.

Pillow handles various image formats, making it versatile for different projects. You can also maintain the aspect ratio by calculating the new dimensions based on the original size, ensuring that the image doesn’t appear stretched or distorted. This method is particularly useful for web applications where image size optimization is crucial for performance.

Method 2: Resizing Images Using OpenCV

OpenCV is another powerful library for image processing, particularly known for its real-time computer vision capabilities. To use OpenCV for resizing images, you need to install it first:

pip install opencv-python

Here’s how you can resize an image using OpenCV:

import cv2

# Read the image
img = cv2.imread("example.jpg")

# Resize the image
img_resized = cv2.resize(img, (800, 600))

# Save the resized image
cv2.imwrite("example_resized.jpg", img_resized)

In this code snippet, we import the cv2 module and read the image file “example.jpg”. The cv2.resize function is used to change the size of the image. Similar to the previous method, you provide the new dimensions as a tuple. After resizing, the image is saved with the name “example_resized.jpg”.

OpenCV allows for more advanced image processing techniques, such as applying filters or transformations. This makes it an excellent choice for projects that require more than just simple resizing. Additionally, OpenCV supports a variety of image formats, ensuring compatibility with most image files you might encounter.

Method 3: Resizing Images with Aspect Ratio

Maintaining the aspect ratio is crucial when resizing images to avoid distortion. Here’s how to resize an image while keeping its original aspect ratio using Pillow:

from PIL import Image

# Open an image file
img = Image.open("example.jpg")

# Calculate the new size while maintaining the aspect ratio
width, height = img.size
aspect_ratio = width / height
new_width = 800
new_height = int(new_width / aspect_ratio)

# Resize the image
img_resized = img.resize((new_width, new_height))

# Save the resized image
img_resized.save("example_resized_aspect_ratio.jpg")

In this example, we first open the image and retrieve its original dimensions. We then calculate the new height based on a desired width of 800 pixels while maintaining the aspect ratio. The image is resized accordingly, and the resized version is saved as “example_resized_aspect_ratio.jpg”.

Output:

Resized image saved as example_resized_aspect_ratio.jpg

This method is particularly useful when you want to ensure that images look natural and not stretched. By calculating the new dimensions based on the original aspect ratio, you can resize images for various applications, from web design to print media, without compromising their visual integrity.

Conclusion

Resizing images in Python is a straightforward process thanks to libraries like Pillow and OpenCV. Whether you need to resize images for web optimization or personal projects, the methods outlined in this guide provide you with the tools to do so effectively. By maintaining the aspect ratio, you can ensure that your images remain visually appealing. With these techniques in your toolkit, you can confidently manipulate images to meet your needs.

FAQ

  1. How do I install the Pillow library?
    You can install Pillow using pip with the command pip install Pillow.

  2. Can I resize images in different formats using Pillow?
    Yes, Pillow supports various image formats, including JPEG, PNG, and BMP.

  3. What is the difference between Pillow and OpenCV?
    Pillow is primarily focused on basic image processing tasks, while OpenCV offers advanced features for computer vision and image analysis.

  4. How do I maintain the aspect ratio when resizing images?
    You can calculate the new dimensions based on the original width and height, ensuring that the ratio remains constant.

  5. Is it possible to resize images in bulk using Python?
    Yes, you can loop through a directory of images and apply the resizing code to each image file programmatically.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Manav Narula
Manav Narula avatar Manav Narula avatar

Manav is a IT Professional who has a lot of experience as a core developer in many live projects. He is an avid learner who enjoys learning new things and sharing his findings whenever possible.

LinkedIn

Related Article - Python Image