File size: 804 Bytes
b89a93f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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