Answer of @Basj as a function:
def resize_image_to_fit_to_box(img: np.array, max_width: int, max_height: int):
f1 = max_width / img.shape[1]
f2 = max_height / img.shape[0]
f = min(f1, f2) # resizing factor
dim = (int(img.shape[1] * f), int(img.shape[0] * f))
resized = cv2.resize(img, dim)
return resized