Image

class toasty.image.Image[source]

Bases: object

A 2D data array stored in memory.

This class primarily exists to help us abstract between the cases where we have “bitmap” RGB(A) images and “science” floating-point images.

Attributes Summary

dtype

height

mode

shape

width

Methods Summary

asarray()

Obtain the image data as a Numpy array.

aspil()

Obtain the image data as PIL.Image.Image.

clear()

Fill the image with whatever “empty” value is most appropriate for its mode.

fill_into_maskable_buffer(buffer, iy_idx, …)

Fill a maskable buffer with a rectangle of data from this image.

from_array(mode, array)

Create a new Image from an array-like data variable.

from_pil(pil_img)

Create a new Image from a PIL image.

make_thumbnail_bitmap()

Create a thumbnail bitmap from the image.

save_default(path_or_stream)

Save this image to a filesystem path or stream

Attributes Documentation

dtype
height
mode
shape
width

Methods Documentation

asarray()[source]

Obtain the image data as a Numpy array.

Returns
If the image is an RGB(A) bitmap, the array will have shape (height, width, planes)
and a dtype of uint8, where planes is either 3
or 4 depending on whether the image has an alpha channel. If the image
is science data, it will have shape (height, width) and have a
floating-point dtype.
aspil()[source]

Obtain the image data as PIL.Image.Image.

Returns
If the image was loaded as a PIL image, the underlying object will be
returned. Otherwise the data array will be converted into a PIL image,
which requires that the array have an RGB(A) format with a shape of
(height, width, planes), where planes is 3 or 4, and a dtype of
uint8.
clear()[source]

Fill the image with whatever “empty” value is most appropriate for its mode.

Notes

The image is assumed to be writable, which will not be the case for images constructed from PIL. If the mode is RGB or RGBA, the buffer is filled with zeros. If the mode is floating-point, the buffer is filled with NaNs.

fill_into_maskable_buffer(buffer, iy_idx, ix_idx, by_idx, bx_idx)[source]

Fill a maskable buffer with a rectangle of data from this image.

Parameters
bufferImage

The destination buffer image, created with ImageMode.make_maskable_buffer().

iy_idxslice or other indexer

The indexer into the Y axis of the source image (self).

ix_idxslice or other indexer

The indexer into the X axis of the source image (self).

by_idxslice or other indexer

The indexer into the Y axis of the destination buffer.

bx_idxslice or other indexer

The indexer into the X axis of the destination buffer.

Notes

This highly specialized function is used to tile images efficiently. No bounds checking is performed. The rectangles defined by the indexers in the source and destination are assumed to agree in size. The regions of the buffer not filled by source data are masked, namely: either filled with alpha=0 or with NaN, depending on the image mode.

classmethod from_array(mode, array)[source]

Create a new Image from an array-like data variable.

Parameters
modeImageMode

The image mode.

arrayarray-like object

The source data.

Returns
A new Image wrapping the data.

Notes

The array will be converted to be at least two-dimensional. The data array shape should match the requirements of the mode.

classmethod from_pil(pil_img)[source]

Create a new Image from a PIL image.

Parameters
pil_imgPIL.Image.Image

The source image.

Returns
A new Image wrapping the PIL image.
make_thumbnail_bitmap()[source]

Create a thumbnail bitmap from the image.

Returns
An RGB PIL.Image.Image representing a thumbnail of the input
image. WWT thumbnails are 96 pixels wide and 45 pixels tall and should
be saved in JPEG format.
save_default(path_or_stream)[source]

Save this image to a filesystem path or stream

Parameters
path_or_streampath-like object or file-like object

The destination into which the data should be written. If file-like, the stream should accept bytes.