- Resize Image While Maintaining Its Aspect Ratio in Python
- 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.
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.
Tkinter, you can interact with the
Tk GUI toolkit in an object-oriented manner. We also import the PIL from
Assume that a picture has been chosen. The size and original image are as follows.
# 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 = Image.open("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 label1.pack() #finally, executing the Tkinter root.mainloop()
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.
#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 Imageheight=100 img_2 = Image.open('bear.jpg') wpercent = (Imagewidth/float(img_2.size)) hsize = int((float(img_2.size)*float(wpercent))) #img = img.resize((mywidth,hsize), PIL.Image.ANTIALIAS) img_2 = img_2.resize((Imagewidth,Imageheight), PIL.Image.Resampling.LANCZOS) img_2.save('resizedImage.jpg') img_2.show()
The following is the original picture.
When we run the code, you can also see the resized image, which was saved in the directory with the name
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.
# 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.open(r"Image Path of the Original Image") #For internal path img_3 = Image.open("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) #img_3=img_3.save(newsize) # Shows the image in the image viewer img_3.show()
The following is the original picture.
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
Image.open 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.