mirror of
https://github.com/immich-app/immich.git
synced 2026-06-17 21:04:51 +08:00
Some checks are pending
CodeQL / Analyze (javascript) (push) Waiting to run
CodeQL / Analyze (python) (push) Waiting to run
Docker / Build and Push (., cpu, server/Dockerfile, immich-server, linux/amd64,linux/arm64) (push) Waiting to run
Docker / Build and Push (machine-learning, armnn, machine-learning/Dockerfile, immich-machine-learning, linux/arm64, -armnn) (push) Waiting to run
Docker / Build and Push (machine-learning, cpu, machine-learning/Dockerfile, immich-machine-learning, linux/amd64,linux/arm64) (push) Waiting to run
Docker / Build and Push (machine-learning, cuda, machine-learning/Dockerfile, immich-machine-learning, linux/amd64, -cuda) (push) Waiting to run
Docker / Build and Push (machine-learning, openvino, machine-learning/Dockerfile, immich-machine-learning, linux/amd64, -openvino) (push) Waiting to run
Docs build / build (push) Waiting to run
Static Code Analysis / Run Dart Code Analysis (push) Waiting to run
Test / Server (push) Waiting to run
Test / CLI (push) Waiting to run
Test / CLI (Windows) (push) Waiting to run
Test / Web (push) Waiting to run
Test / End-to-End Tests (push) Waiting to run
Test / Mobile (push) Waiting to run
Test / Machine Learning (push) Waiting to run
Test / ShellCheck (push) Waiting to run
Test / OpenAPI Clients (push) Waiting to run
Test / TypeORM Checks (push) Waiting to run
* fix(deps): update machine-learning * update openvino options, cuda * update openvino build * fix indentation * update minimum nvidia driver --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: mertalev <101130780+mertalev@users.noreply.github.com>
63 lines
2.1 KiB
Python
63 lines
2.1 KiB
Python
from io import BytesIO
|
|
from typing import IO
|
|
|
|
import cv2
|
|
import numpy as np
|
|
from numpy.typing import NDArray
|
|
from PIL import Image
|
|
|
|
_PIL_RESAMPLING_METHODS = {resampling.name.lower(): resampling for resampling in Image.Resampling}
|
|
|
|
|
|
def resize_pil(img: Image.Image, size: int) -> Image.Image:
|
|
if img.width < img.height:
|
|
return img.resize((size, int((img.height / img.width) * size)), resample=Image.Resampling.BICUBIC)
|
|
else:
|
|
return img.resize((int((img.width / img.height) * size), size), resample=Image.Resampling.BICUBIC)
|
|
|
|
|
|
# https://stackoverflow.com/a/60883103
|
|
def crop_pil(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))
|
|
|
|
|
|
def to_numpy(img: Image.Image) -> NDArray[np.float32]:
|
|
return np.asarray(img if img.mode == "RGB" else img.convert("RGB"), dtype=np.float32) / 255.0
|
|
|
|
|
|
def normalize(
|
|
img: NDArray[np.float32], mean: float | NDArray[np.float32], std: float | NDArray[np.float32]
|
|
) -> NDArray[np.float32]:
|
|
return np.divide(img - mean, std, dtype=np.float32)
|
|
|
|
|
|
def get_pil_resampling(resample: str) -> Image.Resampling:
|
|
return _PIL_RESAMPLING_METHODS[resample.lower()]
|
|
|
|
|
|
def pil_to_cv2(image: Image.Image) -> NDArray[np.uint8]:
|
|
return cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) # type: ignore
|
|
|
|
|
|
def decode_pil(image_bytes: bytes | IO[bytes] | Image.Image) -> Image.Image:
|
|
if isinstance(image_bytes, Image.Image):
|
|
return image_bytes
|
|
image: Image.Image = Image.open(BytesIO(image_bytes) if isinstance(image_bytes, bytes) else image_bytes)
|
|
image.load()
|
|
if not image.mode == "RGB":
|
|
image = image.convert("RGB")
|
|
return image
|
|
|
|
|
|
def decode_cv2(image_bytes: NDArray[np.uint8] | bytes | Image.Image) -> NDArray[np.uint8]:
|
|
if isinstance(image_bytes, bytes):
|
|
image_bytes = decode_pil(image_bytes) # pillow is much faster than cv2
|
|
if isinstance(image_bytes, Image.Image):
|
|
return pil_to_cv2(image_bytes)
|
|
return image_bytes
|