import numpy as np import cv2 from io import BytesIO def resize_by_max(image, max_side=512, force=False): h, w = image.shape[:2] if max(h, w) < max_side and not force: return image ratio = max(h, w) / max_side w = int(w / ratio + 0.5) h = int(h / ratio + 0.5) return cv2.resize(image, (w, h))