Skip to content

Spectral Library

siapy.entities.images.spectral_lib

SpectralLibImage dataclass

SpectralLibImage(file: SpectralLibType)

Bases: ImageBase

Source code in siapy/entities/images/spectral_lib.py
24
25
26
27
28
def __init__(
    self,
    file: "SpectralLibType",
):
    self._file = file

file property

filepath property

filepath: Path

metadata property

metadata: dict[str, Any]

shape property

shape: tuple[int, int, int]

rows property

rows: int

cols property

cols: int

bands property

bands: int

default_bands property

default_bands: list[int]

wavelengths property

wavelengths: list[float]

description property

description: dict[str, Any]

camera_id property

camera_id: str

open classmethod

open(
    *,
    header_path: str | Path,
    image_path: str | Path | None = None,
) -> SpectralLibImage
Source code in siapy/entities/images/spectral_lib.py
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
@classmethod
def open(cls, *, header_path: str | Path, image_path: str | Path | None = None) -> "SpectralLibImage":
    header_path = Path(header_path)
    if not header_path.exists():
        raise InvalidFilepathError(header_path)

    try:
        sp_file = sp.envi.open(file=header_path, image=image_path)
    except Exception as e:
        raise InvalidInputError({"filepath": str(header_path)}, f"Failed to open spectral file: {e}") from e

    if isinstance(sp_file, sp.io.envi.SpectralLibrary):
        raise InvalidInputError({"file_type": type(sp_file).__name__}, "Expected Image, got SpectralLibrary")

    return cls(sp_file)

to_display

to_display(equalize: bool = True) -> Image
Source code in siapy/entities/images/spectral_lib.py
 96
 97
 98
 99
100
101
102
103
104
105
106
def to_display(self, equalize: bool = True) -> Image.Image:
    max_uint8 = 255.0
    image_3ch = self.file.read_bands(self.default_bands)
    image_3ch = self._remove_nan(image_3ch, nan_value=0)
    image_3ch[:, :, 0] = image_3ch[:, :, 0] / image_3ch[:, :, 0].max() / max_uint8
    image_3ch[:, :, 1] = image_3ch[:, :, 1] / (image_3ch[:, :, 1].max() / max_uint8)
    image_3ch[:, :, 2] = image_3ch[:, :, 2] / (image_3ch[:, :, 2].max() / max_uint8)
    image = Image.fromarray(image_3ch.astype("uint8"))
    if equalize:
        image = ImageOps.equalize(image)
    return image

to_numpy

to_numpy(nan_value: float | None = None) -> ndarray
Source code in siapy/entities/images/spectral_lib.py
108
109
110
111
112
def to_numpy(self, nan_value: float | None = None) -> np.ndarray:
    image = self.file[:, :, :]
    if nan_value is not None:
        image = self._remove_nan(image, nan_value)
    return image