from PIL import Image from config import Config import cv2 import numpy as np import math def resize_image_to_1mp(image): """Resizes image to approx 1MP (e.g., 1024x1024) preserving aspect ratio.""" image = image.convert("RGB") w, h = image.size target_pixels = 1024 * 1024 aspect_ratio = w / h # Calculate new dimensions new_h = int((target_pixels / aspect_ratio) ** 0.5) new_w = int(new_h * aspect_ratio) # Ensure divisibility by 48 for efficiency new_w = (new_w // 64) * 64 new_h = (new_h // 64) * 64 if new_w == 0 or new_h == 0: new_w, new_h = 1024, 1024 # Fallback return image.resize((new_w, new_h), Image.LANCZOS)