

LANCZOS ) new_file = output_dir + "/" + outfile + extension img.

open ( input_dir + '/' + infile ) img = img. splitext ( infile ) + "_resized" extension = os. def resize_image ( input_dir, infile, output_dir = "resized", size = ( 320, 180 )): outfile = os.
#Python resize image how to
In this tutorial of Python Examples, we have learned how to use OpenCV cv2.resize() function, to resize an image along width, height or both by preserving the aspect ratio or not preserving the aspect ratio. Output Image cv2.resize() along height or vertical axis Summary import cv2Ĭv2.imwrite('D:/cv2-resize-image-height.png',output) In the dsize, we will keep the width same as that of original image but change the height. Width of the output image remains unchanged from that of the source image. In the following example, we will scale the image only along y-axis or Vertical axis. Output Image cv2.resize() along width or horizontal axis cv2 Resize Image Vertically Output = cv2.resize(src, dsize, interpolation = cv2.INTER_AREA)Ĭv2.imwrite('D:/cv2-resize-image-width.png',output) In the dsize, we will keep the height same as that of original image but change the width.

And we keep the height of the image unchanged. In the following example, we will scale the image only along x-axis or Horizontal axis. Output Image cv2.resize() preserving aspect ratio Example 2: cv2 Resize Image Horizontally Using cv2.imwrite, we are writing the output of cv2.resize to a local image file.cv2.resize resizes the image src to the size dsize and returns numpy array.Then we are setting the desired size dsize with the newly computed width and height.int(src.shape * scale_percent / 100) calculates 50% of the original width. src.shape gives the width of the source image.We are going to scale the image to 50% of its original dimensions, both width and height. cv2.imread() reads the given file in cv2.IMREAD_UNCHANGED with transparency channel (if any) and returns a numpy array with pixel values.What have we done in the above Python program? Height = int(src.shape * scale_percent / 100)Ĭv2.imwrite('D:/cv2-resize-image-50.png',output) Width = int(src.shape * scale_percent / 100) #calculate the 50 percent of original dimensions Src = cv2.imread('D:/cv2-resize-image-original.png', cv2.IMREAD_UNCHANGED) We will resize the image to 50% of its actual shape, i.e., we will reduce its height to 50% of its original and width to 50% of its original. In the following example, we are going to see how we can resize the above image using cv2.resize() while preserving the aspect ratio. Input Image Example 1: Resize Image – cv2.resize() We will use this image as input or source image in our ongoing example programs. Meaning, change height, keeping width same as that of original image.Ĭonsider the following image. Scale the image only along Y-axis or Vertical axis.Meaning, change width, keeping height same as that of original image. Scale the image only along X-axis or Horizontal axis.

Just to make things clear, Aspect Ratio is the ratio of image width to image height.
