Files
Mini-Nav/mini-nav/utils/image.py

22 lines
532 B
Python

"""Image conversion utilities."""
from __future__ import annotations
import numpy as np
from PIL import Image
def numpy_to_pil(rgb: np.ndarray) -> Image.Image:
"""Convert an RGB numpy array to a PIL Image.
Handles arrays with 4 channels (RGBA) by dropping the alpha channel.
Args:
rgb: Numpy array of shape (H, W, C) with dtype uint8 or compatible.
Returns:
PIL Image in RGB mode.
"""
rgb3 = rgb[..., :3] if rgb.shape[-1] > 3 else rgb
return Image.fromarray(rgb3.astype(np.uint8))