Visualization helper functions

sorrel.utils.visualization.render_sprite(world: Gridworld, location: Sequence | None = None, vision: int | None = None, tile_size: list[int] | ndarray = [16, 16]) list[ndarray]

Render a sprite of (2k + 1, 2k + 1) tiles centered at location, where k=vision.

If vision or location is None, render the entire world.map.

Parameters:
  • location – defines the location to centre the visualization on. Defaults to None.

  • vision – defines the size of the visualization of (2v + 1, 2v + 1) pixels. Defaults to None.

  • tile_size – defines the size of the sprites. Default: 16 x 16.

Returns:

A list of np.ndarrays of C x H x W, determined either by the world size or the vision size.

sorrel.utils.visualization.plot(image: ndarray | list[ndarray]) None

Plot helper function that takes an image or list of layers and plots it in Matplotlib.

Parameters:

image – A numpy array or list of numpy arrays with the image layer(s).

sorrel.utils.visualization.image_from_array(image: ndarray | list[ndarray]) Image

Create a PIL image from an single-layer image or list of layers.

Parameters:

image – A numpy array or list of numpy arrays with the image layer(s).

Returns:

A PIL image version of the image.

Return type:

Image

sorrel.utils.visualization.image_from_figure(fig) Image

Convert a Matplotlib figure to a PIL Image.

Note

DO NOT use this with plt.show(), as it will not work and will return a blank image.

Parameters:

fig – If in fig, axis format, then fig. If in plt format, then plt.

Returns:

An image file in PIL format.

Return type:

img

sorrel.utils.visualization.animate_gif(frames: Sequence[PngImageFile], filename: str, folder: str | PathLike) None

Take an array of frames and assemble them into a GIF with the given path.

Parameters:
  • frames – the array of frames

  • filename – A filename to save the images to

  • folder – The path to save the gif to