File size: 708 Bytes
6026000 620e64a 6026000 5f6ad01 6026000 620e64a 6026000 0f0e9c7 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
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)
|