virtualaidresser / utils /image_utils.py
ammar101's picture
Fix: remove over-strict aspect ratio check that blocked T-pose images
cc4e55d
"""
Image preprocessing and validation utilities.
No framework dependencies β€” safe to use in both Streamlit and FastAPI.
"""
import numpy as np
from PIL import Image
def validate_image(image: Image.Image) -> tuple:
"""
Basic sanity checks on the uploaded image.
Returns:
(True, "") if valid
(False, reason_str) if invalid
"""
if image is None:
return False, "No image provided."
w, h = image.size
if w < 100 or h < 100:
return False, "Image resolution too low. Please upload a clear, full-body photo."
if w > h:
return False, (
"Image appears wider than tall. "
"Please upload a portrait-orientation photo (full body visible)."
)
# T-pose images are naturally wider due to arms spread β€” no aspect ratio check.
return True, ""
def pil_to_rgb(image: Image.Image) -> Image.Image:
"""Ensure image is in RGB mode (handles RGBA, grayscale, etc.)."""
return image.convert("RGB")
def pil_to_numpy(image: Image.Image) -> np.ndarray:
"""PIL Image β†’ uint8 numpy array (H, W, 3)."""
return np.array(pil_to_rgb(image))