Skip to main content

ImageGrids

ClassSource

ImageGrids(
n_cols: int,
*,
width: int = 1024,
margins: tuple[int, int] = (0, 0),
gaps: tuple[int, int] = (0, 0),
bg_color: str | tuple[float, ...] | None = None,
fmt: str | ImageFormat = "png"
)
ImageGrids(
n_rows: int,
*,
height: int = 1024,
margins: tuple[int, int] = (0, 0),
gaps: tuple[int, int] = (0, 0),
bg_color: str | tuple[float, ...] | None = None,
fmt: str | ImageFormat = "png"
)
ImageGrids(
n_rows: int,
n_cols: int,
*,
width: int = 1024,
height: int = 1024,
margins: tuple[int, int] = (0, 0),
gaps: tuple[int, int] = (0, 0),
bg_color: str | tuple[float, ...] | None = None,
fmt: str | ImageFormat = "png"
)

The tool used for creating grided images.

Its design is similar to matplotlib's subplots.

Arguments​

Initialization.

ver. 1​

NameTypeRequired
Description
n_colsintThe number of rows in the grid.
widthintThe width of the image. This width will be uniformly divided according to n_cols.
marginstuple[int, int]The (horizontal_margin, vertical_margin) padded to the image.
gapstuple[int, int]The (x_gap, y_gap) defining the separations among images.
bg_colorstr | tuple[float, ...] | NoneThe background color of the whole image. If this value is None, will use transparent background.
fmtstr | ImageFormatThe format of the image. It is used only when saving this image.

ver. 2​

NameTypeRequired
Description
n_rowsintThe number of rows in the grid.
heightintThe height of the image. This height will be uniformly divided according to n_rows.
marginstuple[int, int]The (horizontal_margin, vertical_margin) padded to the image.
gapstuple[int, int]The (x_gap, y_gap) defining the separations among images.
bg_colorstr | tuple[float, ...] | NoneThe background color of the whole image. If this value is None, will use transparent background.
fmtstr | ImageFormatThe format of the image. It is used only when saving this image.

ver. 3​

NameTypeRequired
Description
n_rowsintWhen both rows and cols are specified, this container will has a number limit of specified figures.
n_colsintWhen both rows and cols are specified, this container will has a number limit of specified figures.
widthintThe size of the image. The width and height will be uniformly divided.
heightintThe size of the image. The width and height will be uniformly divided.
marginstuple[int, int]The (horizontal_margin, vertical_margin) padded to the image.
gapstuple[int, int]The (x_gap, y_gap) defining the separations among images.
bg_colorstr | tuple[float, ...] | NoneThe background color of the whole image. If this value is None, will use transparent background.
fmtstr | ImageFormatThe format of the image. It is used only when saving this image.

Methods​

append​

self.append(img: Image | ImageProtocol)

Add an image to this grid.

The image added by this method will be placed after previously registered images.

Requires​

NameTypeRequired
Description
imgImage | ImageProtocolThe image instance to be registered.

clear​

self.clear()

Remove all images registered to this storage.


items​

(v_1: tuple[int, int], v_2: ImageProtocol) in self.items()

Iterate all indicies and corresponding images.

Yields​

NameType
Description
v_1tuple[int, int]The (row, col) index.
v_2ImageProtocolThe image corresponding to the index.

pop​

v_1: ImageProtocol = self.pop(row: int, col: int)

Remove the image in the storage, and return it.

Requires​

NameTypeRequired
Description
rowint
colintThe indicies along the row and column axes.

Returns​

NameType
Description
v_1ImageProtocol

The image removed from the storage.

Will raise KeyError if the given index does not refer to an registered image.

Properties​

img​

img: ImageMultiLayer = self.img

Render the multi-layer image.


n_cols​

n_cols: int = self.n_cols

Get the number of columns based on the currently registered images.


n_rows​

n_rows: int = self.n_rows

Get the number of rows based on the currently registered images.

Operators​

__contains__​

flag: bool = idx in self

Test whether the index exists in the grid.

Requires​

NameTypeRequired
Description
idxtuple[int, int]The (row, col) index.

Returns​

NameType
Description
v_1boolWhether the given index is in the grid.

__delitem__​

del self[idx]

Remove the image in this grid.

Requires​

NameTypeRequired
Description
idxtuple[int, int]The (row, col) index.

__getitem__​

idx: tuple[int, int]
v_1: ImageProtocol = self[idx]

Get the image stored in this grid.

Requires​

NameTypeRequired
Description
idxtuple[int, int]The (row, col) index.

Returns​

NameType
Description
v_1ImageProtocol

The image in the storage.

Will raise KeyError if the given index does not refer to an registered image.


__iter__​

for (v_1, v_2) in self:
v_1: int
v_2: int

Iterate all (row, col) indicies in the order of row-col.

Yields​

NameType
Description
v_1intThe row index.
v_2intThe col index.

__setitem__​

val: Image | ImageProtocol
self[idx] = val

Add the image to this grid.

Requires​

NameTypeRequired
Description
idxtuple[int, int]The (row, col) index.
valImage | ImageProtocol

The image in the to be configured in the storage.

Will raise IndexError if the the given index cannot be placed in the grid.