我知道您只要求 cv2.resize,但您可以使用imutils库,它会为您执行一些比率代码。
import imutils
resized = imutils.resize(img, width=newwidth)
Run Code Online (Sandbox Code Playgroud)
内部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)
Run Code Online (Sandbox Code Playgroud)