File size: 1,162 Bytes
eab789f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cc4e55d
eab789f
 
 
 
 
 
 
 
 
 
 
 
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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
"""
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))