-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathio_tools.py
More file actions
33 lines (24 loc) · 780 Bytes
/
io_tools.py
File metadata and controls
33 lines (24 loc) · 780 Bytes
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
"""Utility helpers for I/O operations used across the pipeline."""
from __future__ import annotations
import io
import logging
from typing import Any
import numpy as np
from PIL import Image, UnidentifiedImageError
log = logging.getLogger(__name__)
def load_image_from_bytes(data: bytes) -> np.ndarray:
"""
Decode ``data`` into an RGB numpy array.
Raises
------
UnidentifiedImageError
If Pillow cannot decode the provided byte buffer.
"""
try:
with Image.open(io.BytesIO(data)) as img:
img.load()
return np.asarray(img.convert("RGB"))
except UnidentifiedImageError:
log.warning("Unidentified image. len=%s first16=%r", len(data), data[:16])
raise
__all__ = ["load_image_from_bytes"]