ml.utils.image

Defines utilities for dealing with images.

ml.utils.image.as_uint8(arr: ndarray) ndarray[source]
ml.utils.image.make_human_viewable_resolution(image: Tensor, interpolation: InterpolationMode = InterpolationMode.BILINEAR, trg_res: tuple[int, int] = (250, 250)) Tensor[source]

Resizes image to human-viewable resolution.

Parameters:
  • image – The image to resize, with shape (C, H, W)

  • interpolation – Interpolation mode to use for image resizing

  • trg_res – The target image resolution; the image will be reshaped to have approximately the same area as an image with this resolution

Returns:

The resized image

ml.utils.image.standardize_image(image: ndarray | Tensor, *, log_key: str | None = None, normalize: bool = True, keep_resolution: bool = False) ndarray[source]

Converts an arbitrary image to shape (C, H, W).

Parameters:
  • image – The image tensor to log

  • log_key – An optional logging key to use in the exception message

  • normalize – Normalize images to (0, 1)

  • keep_resolution – If set, preserve original image resolution, otherwise change image resolution to human-viewable

Returns:

The normalized image, with shape (H, W, C)

Raises:

ValueError – If the image shape is invalid

ml.utils.image.read_gif(in_file: str | Path, *, skip_first_frame: bool = True) Iterator[ndarray][source]

Function that reads a GIF and returns a stream of Numpy arrays.

Parameters:
  • in_file – The path to the input file.

  • skip_first_frame – If set, skip the first frame.

Yields:

A stream of Numpy arrays with shape (H, W, C).

ml.utils.image.write_gif(itr: Iterator[ndarray | Tensor], out_file: str | Path, *, keep_resolution: bool = False, fps: int | Fraction = 10, loop: bool = False, first_frame_zeros: bool = True) None[source]

Function that writes an GIF from a stream of input tensors.

Parameters:
  • itr – The image iterator, yielding images with shape (H, W, C).

  • out_file – The path to the output file.

  • keep_resolution – If set, preserve original image resolution, otherwise change image resolution to human-viewable.

  • fps – Frames per second for the GIF.

  • loop – If set, loop the GIF.

  • first_frame_zeros – If set, the first frame will be all zeros.