Resize Image using Opencv Python

Resize Image using Opencv Python

import cv2filename = ‘your_image.jpg’W = 1000.oriimg = cv2.imread(filename,cv2.CV_LOAD_IMAGE_COLOR)height, width, depth = oriimg.shapeimgScale = W/widthnewX,newY = oriimg.shape[1]*imgScale, oriimg.shape[0]*imgScalenewimg = cv2.resize(oriimg,(int(newX),int(newY)))cv2.imshow(“Show by CV2”,newimg)cv2.waitKey(0)cv2.imwrite(“resizeimg.jpg”,newimg)

Find the Image shape:


It returns a tuple of height,width and channel(if image RGB color)ex:(height,width,channel) as (360,480,3)

Resize the Image:

Option 1:

Syntax:cv2.resize(image,(width,height))Example:img = cv2.resize(oriimg,(360,480))

Option 2(Factor with float value):

Syntax:cv2.resize(image,None,fx=int or float,fy=int or float)fx depends on widthfy depends on heightYou can put the second argument None or (0,0)Example:img = cv2.resize(oriimg,None,fx=0.5,fy=0.5)Note:0.5 means 50% of image to be scalling


Image for post

Different interpolation methods are used to resize the image. It is same syntax but add one argument with key name interpolation.

Preferable interpolation methods are cv.INTER_AREA for shrinking and cv.INTER_CUBIC(slow) & cv.INTER_LINEAR for zooming. By default, interpolation method used is cv.INTER_LINEAR for all resizing purposes. You can resize an input image either of following methods:

Examples:img = cv2.resize(oriimg,None,fx=0.5,fy=0.5,interpolation=cv2.INTER_CUBIC)img = cv2.resize(oriimg,(200,300),interpolation=cv2.INTER_AREA)


No Responses

Write a response