2023-10-31 12:02:04 +02:00
|
|
|
import numpy as np
|
2024-01-13 07:00:09 +02:00
|
|
|
from numpy.typing import NDArray
|
2023-10-31 12:02:04 +02:00
|
|
|
from PIL import Image
|
|
|
|
|
|
|
|
_PIL_RESAMPLING_METHODS = {resampling.name.lower(): resampling for resampling in Image.Resampling}
|
|
|
|
|
|
|
|
|
|
|
|
def resize(img: Image.Image, size: int) -> Image.Image:
|
|
|
|
if img.width < img.height:
|
|
|
|
return img.resize((size, int((img.height / img.width) * size)), resample=Image.BICUBIC)
|
|
|
|
else:
|
|
|
|
return img.resize((int((img.width / img.height) * size), size), resample=Image.BICUBIC)
|
|
|
|
|
|
|
|
|
|
|
|
# https://stackoverflow.com/a/60883103
|
|
|
|
def crop(img: Image.Image, size: int) -> Image.Image:
|
|
|
|
left = int((img.size[0] / 2) - (size / 2))
|
|
|
|
upper = int((img.size[1] / 2) - (size / 2))
|
|
|
|
right = left + size
|
|
|
|
lower = upper + size
|
|
|
|
|
|
|
|
return img.crop((left, upper, right, lower))
|
|
|
|
|
|
|
|
|
2024-01-13 07:00:09 +02:00
|
|
|
def to_numpy(img: Image.Image) -> NDArray[np.float32]:
|
2023-10-31 12:02:04 +02:00
|
|
|
return np.asarray(img.convert("RGB")).astype(np.float32) / 255.0
|
|
|
|
|
|
|
|
|
2024-01-13 07:00:09 +02:00
|
|
|
def normalize(
|
|
|
|
img: NDArray[np.float32], mean: float | NDArray[np.float32], std: float | NDArray[np.float32]
|
|
|
|
) -> NDArray[np.float32]:
|
2023-10-31 12:02:04 +02:00
|
|
|
return (img - mean) / std
|
|
|
|
|
|
|
|
|
|
|
|
def get_pil_resampling(resample: str) -> Image.Resampling:
|
|
|
|
return _PIL_RESAMPLING_METHODS[resample.lower()]
|