

This means you keep the aspect ratio of the image but make the image smaller. One way to change the size of your image is by downscaling it. (new_width, new_height) is the dsize parameter from the original syntax. In other words, we are going to call the cv2.resize() function with the following syntax: cv2.resize(src, (new_width, new_height)) To keep it simple, we are only going to use these two parameters at first: interpolation flag that determines how the output pixels are arranged.fy scale factor along the vertical axis.fx scale factor along the horizontal axis.dsize is the desired size of the output image.The syntax of the cv2.resize() function is: cv2.resize(src, dsize, fx, fy, interpolation) It takes the original image, modifies it, and returns a new image. To resize images with OpenCV, use the cv2.resize() function. Resize an Image with cv2.resize() Function Now that you have read the image, let’s resize it. In this guide, we are working with the following image:įor example, if your script is on the same folder with “image.jpeg” you can read the image into your program by: import cv2 If your Python script is on the same file as the image, you only need to specify the name of the image as the path. The imread() function takes the path of the image as an argument.

To read an image using OpenCV, you need to import the OpenCV library and use the imread() function.
#Opencv resize image how to#
These techniques make the image look as nice as possible when the size changes.īefore modifying images, you need to learn how to read an image to the program in the first place. When using OpenCV, there are multiple techniques you can use to alter the size of an image. Otherwise, scaling up would not be possible because there are not enough pixels to grow the image.

Therefore you have to “remove the excess pixels”.Īnd when scaling up the image, the program has to add new pixels. This is because as the size shrinks, the number of pixels cannot stay the same. When shrinking the image, the pixels need to be resampled. This is because usually, you want to make the output image look the same except for the size. When changing the size of an image, it is important to know the original aspect ratio of the image.
