Change the ratio between width and height of an image using Python - Pillow Last Updated : 23 Dec, 2020 Comments Improve Suggest changes Like Article Like Report Python Imaging Library (expansion of PIL) is the de facto image processing package for the Python language. It incorporates lightweight image processing tools that aid in editing, creating, and saving images. This module is not preloaded with Python. So to install it execute the following command in the command-line: pip install pillowTo change the ratio of height and width we will add or subtract any random value from them. Functions UsedImage.open(fp, mode=’r’) method: This method is used to open the image.Image.resize(size, resample=0) method: This method is used to resize the image. Interpolation happens during the resize process, due to which the quality of image changes whether it is being upscaled (resized to a higher dimension than original) or downscaled (resized to a lower Image then original).Approach:We will first take an image.Then we will find its height and width.Then we will add and subtract any random number from them to change the ratio.Then we will save the image.Example: Image Used: Python3 # Python program to change the ratio of height and # width of an image from PIL import Image # Taking image as input img = Image.open('logo.png') # Getting height and width of the image height = img.size[0] width = img.size[1] # Printing ratio before conversion print('Ratio before conversion:', width/height) # Changing the height and width of the image width = width + 25 height = height - 25 # Resizing the image img = img.resize((width ,height), Image.ANTIALIAS) # Printing the ratio after conversion print('Ratio after conversion:', width/height) # Saving the resized image img.save('Resized Image.png') Output: Ratio before conversion: 1.0 Ratio after conversion: 1.25Output Image: Comment More infoAdvertise with us Next Article Change the ratio between width and height of an image using Python - Pillow aditya_taparia Follow Improve Article Tags : Technical Scripter Python Technical Scripter 2020 Python-pil Practice Tags : python Similar Reads How to find width and height of an image using Python? Finding the width and height of an image in Python means reading the image and determining its dimensions in pixels. For example an image might have a height of 135 pixels and a width of 600 pixels. Letâs explore some common methods to find an imageâs width and height.1. Using OpenCV (cv2)OpenCV is 2 min read How to Concatenate image using Pillow in Python ? Prerequisites: Python Pillow Concatenate image means joining of two images. We can merge any image whether it has different pixels, different image formats namely, 'jpeg', 'png', 'gif', 'tiff', etc. In python, we can join two images using the Python image library also known as the pillow library. In 3 min read Change image resolution using Pillow in Python Prerequisites: Python pillow PIL is the Python Imaging Library which provides the python interpreter with an in-depth file format support, an efficient internal representation, and fairly powerful image processing capabilities. Changing the resolution of an image simply means reducing or increasing 2 min read Getting width and height of an image in Pygame Prerequisites: Pygame To use graphics in python programs we use a module called Pygame. Pygame provides high functionality for developing games and graphics in Python. Nowadays Pygame are very much popular to build simple 2D games. In order to run a program written in Python using Pygame module, a s 3 min read How to merge images with same size using the Python 3 module pillow? In this article, the task is to merge image with size using the module pillow in python 3. Python 3 module pillow : This is the update of Python Imaging Library. It is a free and open-source additional library for the Python programming language that adds support for opening, manipulating, and savi 2 min read PyQtGraph â Getting Fixed Width/Height of Image View In this article, we will see how we can get the fixed width/height of the image view in PyQTGraph. PyQtGraph is a graphics and user interface library for Python that provides functionality commonly required in designing and science applications. Its primary goals are to provide fast, interactive gra 4 min read How to rotate an image by an angle using PyTorch in Python? In this article, we are going to see how to rotate an image by an angle in PyTorch. To achieve this, we can use RandomRotation() method. RandomRotation() transform accepts both PIL and tensor images. A Tensor Image is a tensor with (C, H, W) shape, C is for the number of channels, H and W are for th 2 min read Generate square or circular thumbnail image with Python - Pillow Prerequisites: Pillow, Numpy In the word "THUMBNAIL" the word THUMB means short. A thumbnail is the compressed preview image of the original image or a Thumbnail is the smaller version of an image. In simple words, the thumbnail is the smaller image that represents the larger/original image. Usuall 6 min read Convert an image into jpg format using Pillow in Python Let us see how to convert an image into jpg format in Python. The size of png is larger when compared to jpg format. We also know that some applications might ask for images of smaller sizes. Hence conversion from png(larger ) to jpg(smaller) is needed. For this task we will be using the Image.conve 2 min read Expanding the Canvas Without Resizing in Python Pillow The Python Imaging Library (PIL) is a powerful tool for image processing in Python, offering a wide range of functionalities from basic image manipulations to complex operations. One of the useful features of PIL is the ability to expand the canvas of an image without resizing the original content. 4 min read Like