insiders-generation-hub / image_preprocessing.py
obsxrver
Support HEIC image uploads with standardized decoding
b89a93f
Raw History Blame Contribute Delete
804 Bytes
from pathlib import Path
from typing import Union
from PIL import Image, ImageOps
from pillow_heif import register_heif_opener
# Register HEIC/HEIF decoding globally so Pillow-based consumers such as Gradio
# can accept iPhone uploads as well as the preprocessing function below.
register_heif_opener(thumbnails=False)
def load_standardized_image(image_path: Union[str, Path]) -> Image.Image:
"""Load an image with its orientation applied and normalize it to RGB."""
with Image.open(image_path) as image:
image.load()
oriented_image = ImageOps.exif_transpose(image)
standardized_image = oriented_image.convert("RGB")
# Return a detached image without source metadata, including EXIF orientation.
standardized_image.info.clear()
return standardized_image