Skip to content

Image I/O

plakakia.io

Image and N-D array reading/writing.

Images go through OpenCV. Files are decoded from bytes (np.fromfile + cv2.imdecode) rather than with cv2.imread so that non-ASCII paths work on every platform, and failures raise exceptions instead of silently returning None.

N-D arrays (hyperspectral cubes, multi-band rasters) are read from .npy, HDF5 and TIFF files, and GeoTIFFs can be read and written with their georeferencing through rasterio.

GeoProfile dataclass

GeoProfile(
    crs: Any, transform: Any, nodata: float | None = None
)

Georeferencing of a raster: coordinate reference system, affine transform, nodata.

transform maps (col, row) pixel coordinates to map coordinates.

for_window

for_window(col_off: int, row_off: int) -> GeoProfile

Profile of a sub-window starting at pixel (col_off, row_off).

read_image

read_image(
    path: PathLike, *, flags: int = cv2.IMREAD_UNCHANGED
) -> np.ndarray

Read an image from disk.

By default the image is read unchanged, which preserves grayscale, alpha and 16-bit data. Colour images are returned in OpenCV's BGR channel order.

Raises:

Type Description
FileNotFoundError

If path does not exist.

ValueError

If the file cannot be decoded as an image.

write_image

write_image(
    path: PathLike,
    image: ndarray,
    params: Sequence[int] = (),
) -> None

Write an image to disk; the format is inferred from the file extension.

Raises:

Type Description
OSError

If OpenCV cannot encode the image in the requested format.

pad_image

pad_image(
    image: ndarray,
    pad: int,
    value: float = 0,
    *,
    axes: Sequence[int] = (0, 1),
) -> np.ndarray

Pad an array with a constant border of pad pixels along axes.

Works for any number of dimensions and channels (unlike cv2.copyMakeBorder).

read_array

read_array(
    path: PathLike,
    *,
    hdf5_dataset: str | None = None,
    mmap: bool = True,
) -> np.ndarray

Read an image or N-D array (e.g. a hyperspectral cube) from disk.

The reader is chosen from the extension:

  • .npy: NumPy, memory-mapped by default so only the tiles you use are read.
  • .h5 / .hdf5 / .he5: HDF5 via h5py (plakakia[hyperspectral]). hdf5_dataset names the dataset to read; it may be omitted when the file holds a single dataset. The file is closed before returning.
  • .tif / .tiff: tifffile when installed (multi-band TIFF/GeoTIFF), otherwise OpenCV.
  • anything else: OpenCV, see :func:read_image.

The array is returned as stored; no axis is assumed to hold bands.

write_array

write_array(
    path: PathLike,
    array: ndarray,
    *,
    hdf5_dataset: str | None = None,
) -> None

Write an array with the writer matching the extension (see :func:read_array).

.npy and HDF5 accept any shape and dtype; TIFF uses tifffile when installed; other extensions go through OpenCV (:func:write_image).

read_geotiff

read_geotiff(
    path: PathLike,
) -> tuple[np.ndarray, GeoProfile]

Read a (multi-band) GeoTIFF with rasterio (plakakia[geo]).

Returns:

Type Description
ndarray

(array, profile) where array is (bands, height, width), rasterio's

GeoProfile

native layout, so tile it with axes=(1, 2).

write_geotiff

write_geotiff(
    path: PathLike, array: ndarray, profile: GeoProfile
) -> None

Write a (bands, height, width) (or (height, width)) array as a GeoTIFF.