![]() ![]() 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. ![]() 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. Preserve the Aspect Ration and increase or decrease the width and height of the image.You can think interpolation as a method that decides which pixel gets which value based on its neighboring pixels and the scale at which the image is being resized. interpolation could be one of the following values.īased on the interpolation technique selected, respective algorithm is used.fy is the scaling factor along Y-axis or Vertical axis.fx is the scaling factor along X-axis or Horizontal axis.dsize is the desired size of the output image, given as tuple.src is the source, original or input image in the form of numpy array.Syntax of cv2 resize() functionįollowing is the syntax of cv2.resize() function. In this tutorial, we shall learn how to resize image in Python using OpenCV library. Aspect Ratio can be preserved by calculating width or height for given target height or width respectively. The aspect ratio can be preserved or not, based on the requirement. Resizing, by default, does only change the width and height of the image. Opencv imread method read image and return numpy array, and Size of numpy array equal to image array.To resize an image in Python, you can use cv2.resize() function of OpenCV library cv2. Newimage = cv2.resize(oriimage,(newx,newy)) Newx,newy = oriimage.shape/4,oriimage.shape/4 #new size (w,h) If you don't specify a size (by using None), then it expects the X and Y scaling factors OpenCV provides a function called resize to achieve image scaling. when you open the image with image viewer it open image in fixed window size and window size don't depend on image pixel When you show the resized image with imshow() it shows the image on-screen and change showing window size according to an image pixel. ![]()
0 Comments
Leave a Reply. |