Skip to content

Images

siapy.entities.images

GeometricShapes dataclass

GeometricShapes(image: SpectralImage, geometric_shapes: list[Shape] | None = None)
PARAMETER DESCRIPTION
image

TYPE: SpectralImage

geometric_shapes

TYPE: list[Shape] | None DEFAULT: None

Source code in siapy/entities/images.py
29
30
31
32
33
34
35
def __init__(
    self, image: "SpectralImage", geometric_shapes: list["Shape"] | None = None
):
    self._image = image
    self._geometric_shapes = (
        geometric_shapes if geometric_shapes is not None else []
    )

shapes property writable

shapes: list[Shape]

append

append(shape: Shape)
PARAMETER DESCRIPTION
shape

TYPE: Shape

Source code in siapy/entities/images.py
67
68
69
def append(self, shape: "Shape"):
    self._check_shape_type(shape)
    self._geometric_shapes.append(shape)

extend

extend(shapes: Iterable[Shape])
PARAMETER DESCRIPTION
shapes

TYPE: Iterable[Shape]

Source code in siapy/entities/images.py
71
72
73
def extend(self, shapes: Iterable["Shape"]):
    self._check_shape_type(shapes)
    self._geometric_shapes.extend(shapes)

insert

insert(index: int, shape: Shape)
PARAMETER DESCRIPTION
index

TYPE: int

shape

TYPE: Shape

Source code in siapy/entities/images.py
75
76
77
def insert(self, index: int, shape: "Shape"):
    self._check_shape_type(shape)
    self._geometric_shapes.insert(index, shape)

remove

remove(shape: Shape)
PARAMETER DESCRIPTION
shape

TYPE: Shape

Source code in siapy/entities/images.py
79
80
81
def remove(self, shape: "Shape"):
    self._check_shape_type(shape)
    self._geometric_shapes.remove(shape)

pop

pop(index: int = -1) -> Shape
PARAMETER DESCRIPTION
index

TYPE: int DEFAULT: -1

Source code in siapy/entities/images.py
83
84
def pop(self, index: int = -1) -> "Shape":
    return self._geometric_shapes.pop(index)

clear

clear()
Source code in siapy/entities/images.py
86
87
def clear(self):
    self._geometric_shapes.clear()

index

index(shape: Shape, start: int = 0, stop: int = sys.maxsize) -> int
PARAMETER DESCRIPTION
shape

TYPE: Shape

start

TYPE: int DEFAULT: 0

stop

TYPE: int DEFAULT: maxsize

Source code in siapy/entities/images.py
89
90
91
def index(self, shape: "Shape", start: int = 0, stop: int = sys.maxsize) -> int:
    self._check_shape_type(shape)
    return self._geometric_shapes.index(shape, start, stop)

count

count(shape: Shape) -> int
PARAMETER DESCRIPTION
shape

TYPE: Shape

Source code in siapy/entities/images.py
93
94
95
def count(self, shape: "Shape") -> int:
    self._check_shape_type(shape)
    return self._geometric_shapes.count(shape)

reverse

reverse()
Source code in siapy/entities/images.py
97
98
def reverse(self):
    self._geometric_shapes.reverse()

sort

sort(key: Any = None, reverse: bool = False)
PARAMETER DESCRIPTION
key

TYPE: Any DEFAULT: None

reverse

TYPE: bool DEFAULT: False

Source code in siapy/entities/images.py
100
101
def sort(self, key: Any = None, reverse: bool = False):
    self._geometric_shapes.sort(key=key, reverse=reverse)

get_by_name

get_by_name(name: str) -> Shape | None
PARAMETER DESCRIPTION
name

TYPE: str

Source code in siapy/entities/images.py
103
104
105
106
107
def get_by_name(self, name: str) -> Shape | None:
    names = [shape.label for shape in self.shapes]
    if name in names:
        index = names.index(name)
        return self.shapes[index]

SpectralImage dataclass

SpectralImage(sp_file: SpectralType, geometric_shapes: list[Shape] | None = None)
PARAMETER DESCRIPTION
sp_file

TYPE: SpectralType

geometric_shapes

TYPE: list[Shape] | None DEFAULT: None

Source code in siapy/entities/images.py
135
136
137
138
139
def __init__(
    self, sp_file: "SpectralType", geometric_shapes: list["Shape"] | None = None
):
    self._sp_file = sp_file
    self._geometric_shapes = GeometricShapes(self, geometric_shapes)

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

geometric_shapes property

geometric_shapes: GeometricShapes

envi_open classmethod

envi_open(*, header_path: str | Path, image_path: str | Path | None = None) -> SpectralImage
PARAMETER DESCRIPTION
header_path

TYPE: str | Path

image_path

TYPE: str | Path | None DEFAULT: None

Source code in siapy/entities/images.py
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
@classmethod
def envi_open(
    cls, *, header_path: str | Path, image_path: str | Path | None = None
) -> "SpectralImage":
    if not Path(header_path).exists():
        raise InvalidFilepathError(str(header_path))
    sp_file = sp.envi.open(file=header_path, image=image_path)
    if isinstance(sp_file, sp.io.envi.SpectralLibrary):
        raise InvalidInputError(
            {
                "file_type": type(sp_file).__name__,
            },
            "Opened file of type SpectralLibrary",
        )
    return cls(sp_file)

to_display

to_display(equalize: bool = True) -> Image
PARAMETER DESCRIPTION
equalize

TYPE: bool DEFAULT: True

Source code in siapy/entities/images.py
228
229
230
231
232
233
234
235
236
237
238
def to_display(self, equalize: bool = True) -> Image.Image:
    max_uint8 = 255.0
    image_3ch = self._sp_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
PARAMETER DESCRIPTION
nan_value

TYPE: float | None DEFAULT: None

Source code in siapy/entities/images.py
240
241
242
243
244
def to_numpy(self, nan_value: float | None = None) -> np.ndarray:
    image = self._sp_file[:, :, :]
    if nan_value is not None:
        image = self._remove_nan(image, nan_value)
    return image

to_signatures

to_signatures(pixels: Pixels) -> Signatures
PARAMETER DESCRIPTION
pixels

TYPE: Pixels

Source code in siapy/entities/images.py
246
247
248
249
def to_signatures(self, pixels: "Pixels") -> Signatures:
    image_arr = self.to_numpy()
    signatures = Signatures.from_array_and_pixels(image_arr, pixels)
    return signatures

to_subarray

to_subarray(pixels: Pixels) -> ndarray
PARAMETER DESCRIPTION
pixels

TYPE: Pixels

Source code in siapy/entities/images.py
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
def to_subarray(self, pixels: "Pixels") -> np.ndarray:
    image_arr = self.to_numpy()
    u_max = pixels.u().max()
    u_min = pixels.u().min()
    v_max = pixels.v().max()
    v_min = pixels.v().min()
    # create new image
    image_arr_area = np.nan * np.ones(
        (v_max - v_min + 1, u_max - u_min + 1, self.bands)
    )
    # convert original coordinates to coordinates for new image
    v_norm = pixels.v() - v_min
    u_norm = pixels.u() - u_min
    # write values from original image to new image
    image_arr_area[v_norm, u_norm, :] = image_arr[pixels.v(), pixels.u(), :]
    return image_arr_area

mean

mean(axis: int | tuple[int, ...] | Sequence[int] | None = None) -> float | ndarray
PARAMETER DESCRIPTION
axis

TYPE: int | tuple[int, ...] | Sequence[int] | None DEFAULT: None

Source code in siapy/entities/images.py
268
269
270
271
272
def mean(
    self, axis: int | tuple[int, ...] | Sequence[int] | None = None
) -> float | np.ndarray:
    image_arr = self.to_numpy()
    return np.nanmean(image_arr, axis=axis)