Resize Image While Maintaining Its Aspect Ratio in Python

Resize Image While Maintaining Its Aspect Ratio in Python

  1. Resize Image While Maintaining Its Aspect Ratio in Python
  2. Crop Image by Maintaining Its Aspect Ratio in Python

The purpose of this Python article is to explain how we can resize an image in Python while maintaining its aspect ratio. The method of resizing an image in Python will also describe its usage with a proper example program.

Resize Image While Maintaining Its Aspect Ratio in Python

In Python, you can resize an image with the help of some predefined packages. By importing those packages and using important functions and methods, you can resize an Image in Python without losing its aspect ratio.

Use Python Imaging Library (PIL)

Python’s imaging library, PIL, is a Python library for imaging.

The purpose of a script is to automate system-oriented tasks. Scripting is when you write code for practical tasks without needing to compile it.

In addition to its powerful image processing capabilities, PIL is capable of handling a wide variety of image formats ( including BMP, DIB, EPS, GIF, ICO, IM, MSP, PCX, PNG, PPM, SGI, TGA, TIFF, WebP, XBM) and image modes such as RGB, RGBA, B&W, and monochrome.

Additionally, PIL is compatible with most operating systems, including Windows, Linux, and macOS.

The standard GUI library for Python is Tkinter. When Python is combined with Tkinter, GUI applications can be created quickly and easily.

With Tkinter, you can interact with the Tk GUI toolkit in an object-oriented manner. We also import the PIL from Tkinter.

Assume that a picture has been chosen. The size and original image are as follows.

Original image that we want to resize

Example Code:

# First, we have to import the modules here
from tkinter import *
from PIL import Image, ImageTk
# Now we are creating the object
root = Tk()
# Here, just reading the Image
image ="koala.png")
# Here now, using resize() method, resizing the image
resize_originalimage = image.resize((140, 80))
img_1 = ImageTk.PhotoImage(orgnlimg)
# creating, add resize image, and labeling it
label1 = Label(image=img_1)
label1.image = img_1
#finally, executing the Tkinter


Resize Image Output

Whether an image is displayed on a screen or stored in an image file, it is represented by discrete pixels. An aliasing effect occurs when the image data is not the same resolution as the screen representation.

Subsampling reduces aliasing by smoothing the data and then subsampling the smoothed data. In this case, ANTIALIAS is used.

Resampling.LANCZOS is a method of interpolating sampled data to produce new values. The method is commonly used in multivariate interpolation, such as resizing digital images.

The original Image size is 284 x 606, and we will also be able to tell the difference between the original image and the resized image once the code executes.

Example Code:

#Python Code to resize an image while maintaining the original aspect ratio, using the PIL Methods
import PIL
from PIL import Image
#setting the size of resized image
Imagewidth = 80
img_2 ='bear.jpg')
wpercent = (Imagewidth/float(img_2.size[0]))
hsize = int((float(img_2.size[1])*float(wpercent)))
#img = img.resize((mywidth,hsize), PIL.Image.ANTIALIAS)
img_2 = img_2.resize((Imagewidth,Imageheight), PIL.Image.Resampling.LANCZOS)'resizedImage.jpg')

The following is the original picture.

Original Size of the picture


The output of the image resized

When we run the code, you can also see the resized image, which was saved in the directory with the name resizedImage.jpg.

Crop Image by Maintaining Its Aspect Ratio in Python

The following is the resize and new size method used from PIL, cropping the image instead of resizing it.

Example Code:

# Importing Image class from PIL module by maintaining its aspect ratio
from PIL import Image
# Opens an image in RGB mode
#For the external path, we use this :
#img_3 ="Image Path of the Original Image")
#For internal path
img_3 ="cat.png")
# The size of the image in pixels (size of the original image)
# (This is not mandatory)
width, height = img_3.size
# Setting the cropped image points
left = 4
top = height / 5
right = 154
bottom = 3 * height / 5
# Cropped image of the above dimension
# (It will not change the original image)
img_3 = img_3.crop((left, top, right, bottom))
newsize = (300, 300)
img_3 = img_3.resize(newsize)
# Shows the image in the image viewer

The following is the original picture.

Original Size of the picture


The output of the cropped cat image

Since we see the cropped image as an output of the code, we can assume that PIL is not just for resizing an image by maintaining its aspect ratio but also for cropping.

With Python, we used to resize and read an image using PIL and maintain its aspect ratio. After calculating the new width and height, we resized the image using the resize method and saved the new image using the same method according to the new width.

We hope you find this article helpful in understanding how to resize an image in Python.

Author: Abid Ullah
Abid Ullah avatar Abid Ullah avatar

My name is Abid Ullah, and I am a software engineer. I love writing articles on programming, and my favorite topics are Python, PHP, JavaScript, and Linux. I tend to provide solutions to people in programming problems through my articles. I believe that I can bring a lot to you with my skills, experience, and qualification in technical writing.


Related Article - Python Image

  • Python Image Compression
  • Perform Image Segmentation in Python
  • Image Resize in Python
  • Resize Image in Python
  • Download Image in Python