stouputils.image.numpy_to_gif module#

numpy_to_gif(
path: str,
array: NDArray[np.integer | np.floating | np.bool_],
duration: int = 100,
loop: int = 0,
mkdir: bool = True,
**kwargs: Any,
) None[source]#

Generate a ‘.gif’ file from a numpy array for 3D/4D visualization.

Parameters:
  • path (str) – Path to the output .gif file.

  • array (NDArray) – Numpy array to be dumped (must be 3D or 4D). 3D: (depth, height, width) - e.g. (64, 1024, 1024) 4D: (depth, height, width, channels) - e.g. (50, 64, 1024, 3)

  • duration (int) – Duration between frames in milliseconds.

  • loop (int) – Number of loops (0 = infinite).

  • mkdir (bool) – Create the directory if it does not exist.

  • **kwargs (Any) – Additional keyword arguments for PIL.Image.save().

Examples

> # 3D array example
> array = np.random.randint(0, 256, (10, 100, 100), dtype=np.uint8)
> numpy_to_gif("output_10_frames_100x100.gif", array, duration=200, loop=0)

> # 4D array example (batch of 3D images)
> array_4d = np.random.randint(0, 256, (5, 10, 100, 3), dtype=np.uint8)
> numpy_to_gif("output_50_frames_100x100.gif", array_4d, duration=200)

> total_duration = 1000  # 1 second
> numpy_to_gif("output_1s.gif", array, duration=total_duration // len(array))