3

I want to resize my image with given one edge's new length by using cv2.resize() method.

For example my image has dimension 300x400, and I want to increase my height from 400 to 500 but I should write only one edge's new size(just 500), the other edge should automatically be increased. What is sequence?

Jeru Luke
  • 20,118
  • 13
  • 80
  • 87
user70
  • 597
  • 2
  • 7
  • 24

2 Answers2

6

I know you are asking for cv2.resize only, but you could use the imutils library which does some of the ratio code for you.

import imutils
resized = imutils.resize(img, width=newwidth)

Inside imutils:

dim = None
(h, w) = image.shape[:2]

# check to see if the width is None
if width is None:
    # calculate the ratio of the height and construct the
    # dimensions
    r = height / float(h)
    dim = (int(w * r), height)

# otherwise, the height is None
else:
    # calculate the ratio of the width and construct the
    # dimensions
    r = width / float(w)
    dim = (width, int(h * r))

# resize the image
resized = cv2.resize(image, dim, interpolation=inter)
Benehiko
  • 462
  • 3
  • 6
1

You can resize with the fx and fy parameters of the resize function.

img = cv2.resize(img, (0,0), fx=500/400, fy=500/400)
Sunreef
  • 4,452
  • 21
  • 33