def rescale_by_height(image, target_height, method=cv2.INTER_LANCZOS4):
"""Rescale `image` to `target_height` (preserving aspect ratio)."""
w = int(round(target_height * image.shape[1] / image.shape[0]))
return cv2.resize(image, (w, target_height), interpolation=method)
def rescale_by_width(image, target_width, method=cv2.INTER_LANCZOS4):
"""Rescale `image` to `target_width` (preserving aspect ratio)."""
h = int(round(target_width * image.shape[0] / image.shape[1]))
return cv2.resize(image, (target_width, h), interpolation=method)