Resizing image using python

2 mn read

Resizing images is a common task in computer vision and image processing applications. In Python, there are several libraries and modules that can be used to resize images, such as OpenCV, Pillow, and scikit-image. In this article, we will focus on using the Pillow library to resize images in Python.
The Pillow library is a popular Python library for working with images. It provides a wide range of functions and methods for loading, manipulating, and saving images in various formats. To use the Pillow library, you will need to install it on your system. You can do this using the pip package manager, by running the following command:

pip install Pillow

Once the Pillow library is installed, you can use it to resize images in Python. Here is an example of how you might use the Pillow library to resize an image:

from PIL import Image
# Open the image file
image = Image.open('original.jpg')
# Resize the image
image = image.resize((800,600))
# Save the resized image
image.save('resized.jpg')

This code opens an image file, resizes it to a specified width and height, and then saves the resized image to a new file. The resize() method takes a tuple as an argument, which specifies the width and height of the resized image. In this example, the image is resized to 800 pixels wide and 600 pixels tall.

You can customize this code to suit your specific needs and requirements. For example, you can adjust the width and height of the resized image to different values, or you can use the original aspect ratio of the image to calculate the width and height of the resized image. Here is an example of how you might do this:

# Get the original width and height of the image
width, height = image.size
# Calculate the new width and height using the original aspect ratio
new_width = 800
new_height = int(new_width * height / width)
# Resize the image to the new width and height
image = image.resize((new_width, new_height))

This code calculates the new width and height of the resized image using the original aspect ratio of the image. It then uses the resize() method to resize the image to the new width and height.

Leave a Reply

Your email address will not be published. Required fields are marked *

Reading is essential for those who seek to rise above the ordinary.

Discover Gorgo

Welcome to Gorgo, an author oriented theme.
A place where words matter. Discover without further ado our countless community stories.

Build great relations

Explore all the content form Gorgo community network. Forums, Groups, Members, Posts, Social Wall and many more. You can never get tired of it!

Become a member

Get unlimited access to the best articles on Gorgo and support our  lovely authors. Upgrade Now