GitLab Repo

amachine.am_visualization.am_to_video

 1import imageio.v2 as iio
 2
 3from matplotlib.colors import Normalize
 4from matplotlib import colormaps
 5from matplotlib.colors import Colormap
 6
 7import numpy as np
 8
 9def to_video(
10    images      : list[np.ndarray],
11    output_path : str = "output.mp4",
12    fps         : int = 3,
13    cmap        : str | Colormap = "plasma_r",
14) -> None:
15    if not images:
16        raise ValueError("images list is empty")
17
18    colormap = colormaps[cmap] if isinstance(cmap, str) else cmap
19
20    # Global min/max so colormapping is consistent across frames
21    global_min = min(f.min() for f in images)
22    global_max = max(f.max() for f in images)
23
24    if global_min == global_max:
25        raise ValueError("All frames are constant, cannot normalize")
26
27    norm = Normalize( vmin=global_min, vmax=global_max )
28
29    with iio.get_writer(output_path, fps=fps) as writer:
30        for frame in images:
31            rgba   = colormap(norm(frame))
32            rgb    = (rgba[..., :3] * 255).astype(np.uint8)
33            writer.append_data(rgb)
def to_video( images: list[numpy.ndarray], output_path: str = 'output.mp4', fps: int = 3, cmap: str | matplotlib.colors.Colormap = 'plasma_r') -> None:
10def to_video(
11    images      : list[np.ndarray],
12    output_path : str = "output.mp4",
13    fps         : int = 3,
14    cmap        : str | Colormap = "plasma_r",
15) -> None:
16    if not images:
17        raise ValueError("images list is empty")
18
19    colormap = colormaps[cmap] if isinstance(cmap, str) else cmap
20
21    # Global min/max so colormapping is consistent across frames
22    global_min = min(f.min() for f in images)
23    global_max = max(f.max() for f in images)
24
25    if global_min == global_max:
26        raise ValueError("All frames are constant, cannot normalize")
27
28    norm = Normalize( vmin=global_min, vmax=global_max )
29
30    with iio.get_writer(output_path, fps=fps) as writer:
31        for frame in images:
32            rgba   = colormap(norm(frame))
33            rgb    = (rgba[..., :3] * 255).astype(np.uint8)
34            writer.append_data(rgb)