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

default_format

dtype

height

mode

shape

wcs

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(array[, wcs, default_format])

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

from_pil(pil_img[, wcs, default_format])

Create a new Image from a PIL image.

make_thumbnail_bitmap()

Create a thumbnail bitmap from the image.

save(path_or_stream[, format, mode])

Save this image to a filesystem path or stream

update_into_maskable_buffer(buffer, iy_idx, …)

Update a maskable buffer with data from this image.

Attributes Documentation

default_format
dtype
height
mode
shape
wcs
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(array, wcs=None, default_format=None)[source]

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

Parameters
arrayarray-like object

The source data. This should either be a 2-d floating point array a 3-d floating-point or uint8 array with shape (3, ny, nx), or a 3-d uint8 array with shape (4, ny, nx).

wcsWCS, optional

The WCS coordinate system for the image.

default_formatstr, optional

The default format to use when writing the image if none is specified explicitly. If not specified, this is automatically chosen at write time based on the array type.

Returns
A new Image wrapping the data.

Notes

The array will be converted to be at least two-dimensional.

classmethod from_pil(pil_img, wcs=None, default_format=None)[source]

Create a new Image from a PIL image.

Parameters
pil_imgPIL.Image.Image

The source image.

wcsWCS, optional

The WCS coordinate system for the image.

default_formatstr, optional

The default format to use when writing the image if none is specified explicitly.

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(path_or_stream, format=None, mode=None)[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.

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

Update a maskable buffer with 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

Unlike fill_into_maskable_buffer(), this function does not clear the entire buffer. It only overwrites the portion of the buffer covered by non-NaN-like values of the input image.