In the middle, we have resized the image to half its size - and other than the image being resized, there is no loss in image “quality.” However, on the right, we have dramatically increased the image size. In terms of “quality” of the output image, decreasing an image’s size is always easier (and more aesthetically pleasing) than increasing it. On the other hand, if we were to increase the size of the image, the interpolation function would have to “fill in the gaps” between pixels that previously did not exist.įor example, take a look at the image in Figure 2:įigure 2: Increasing and decreasing the size of an image with OpenCV. This is because the interpolation function simply has to remove pixels from an image. In general, it’s far more beneficial (and visually appealing) to decrease the size of the image. And the goal of an interpolation function is to take these neighborhoods of pixels and use them to either increase or decrease the size of the image. In this case, the “known points” are the pixels of our original image. The formal definition of interpolation is:Ī method of constructing new data points within the range of a discrete set of known data points. We also need to keep in mind the interpolation method of our resizing function. As we explore machine learning/deep learning techniques, we’ll find that our internal algorithms often ignore the aspect ratio of an image but more on that once we understand the fundamentals of computer vision. Exceptions most certainly do apply, though. In general, you’ll want to preserve the images’ aspect ratio when resizing - especially if these images are presented as output to the user. They have been resized by ignoring the ratio of the width to the height of the image. And on the right, we have two images that have been distorted by not preserving the aspect ratio. Figure 1: Ignoring the aspect ratio of an image can lead to resized images that look distorted, squished, and crunched. resize((target_width, new_height), Image. Target_width = 150 # target_width * original_height / original_width In this case, you know the target width, so the new height need to be calculated to maintain its aspect ratio. resize((new_width, target_height), Image. Target_height = 200 # target_height * original_width / original_height In this case, you know the target height, now you need calculate the new width to maintain the aspect ratio of original image. Resize image by some percentage of the original size, so it can maintain the same aspect ratio. If you only want to decrease the size of an image, you can use the thumbnail() method.ġ. It provides resize() method to resize image to any target size, both for increase and decrease the size. Use PIL (Python Imaging Library) to resize the image.
How to resize an image and keep its aspect ratio Resize an image and keep its aspect ratio.Calculate Euclidean distance btween two points.Python ImportError and ModuleNotFoundError.Indefinitely Request User Input Until a Valid Response.Test Single Variable in Multiple Values.Create variables dynamically in while loop.Python Global Variables and Global Keyword.Filter dictionary to contain specific keys.Get key with maximum value in dictionary.Create dictionary with dict comprehension and iterables.Copy a dictionary and edit the copy only.Convert a String representation of a Dictionary to a dictionary.Extract specific element from each sublist.Find the intersection between two lists.Find the cumulative sum of numbers in a list.