Ejemplo n.º 1
0
def create_gif(figure,
               file_name,
               n_frames=60,
               zoom=1,
               z_offset=0.5,
               size=(600, 600),
               rotate_forward=True):
    """
    Convert a Fury Scene object into a gif

    Parameters
    ----------
    figure: Fury Scene object
        Scene to be converted to a gif

    file_name: str
        File to save gif to.

    n_frames: int, optional
        Number of frames in gif.
        Will be evenly distributed throughout the rotation.
        Default: 60

    zoom: int, optional
        How much to magnify the figure in the fig.
        Default: 1

    size: tuple, optional
        Size of the gif.
        Default: (600, 600)

    rotate_forward: bool, optional
        Whether to rotate the figure forward before converting to a gif.
        Generally necessary for fury scenes.
        Default: True
    """
    if rotate_forward:
        figure = scene_rotate_forward(figure)

    tdir = tempfile.gettempdir()
    window.record(figure,
                  az_ang=360.0 / n_frames,
                  n_frames=n_frames,
                  path_numbering=True,
                  out_path=tdir + '/tgif',
                  magnification=zoom,
                  size=size)

    vut.gif_from_pngs(tdir,
                      file_name,
                      n_frames,
                      png_fname="tgif",
                      add_zeros=True)
Ejemplo n.º 2
0
def create_gif(figure,
               file_name,
               n_frames=30,
               zoom=2.5,
               z_offset=0.5,
               size=(600, 600)):
    """
    Convert a Plotly Figure object into a gif

    Parameters
    ----------
    figure: Plotly Figure object
        Figure to be converted to a gif

    file_name: str
        File to save gif to.

    n_frames: int, optional
        Number of frames in gif.
        Will be evenly distributed throughout the rotation.
        Default: 60

    zoom: float, optional
        How much to magnify the figure in the fig.
        Default: 2.5

    size: tuple, optional
        Size of the gif.
        Default: (600, 600)
    """
    tdir = tempfile.gettempdir()

    for i in range(n_frames):
        theta = (i * 6.28) / n_frames
        camera = dict(eye=dict(
            x=np.cos(theta) * zoom, y=np.sin(theta) * zoom, z=z_offset))
        figure.update_layout(scene_camera=camera)
        figure.write_image(tdir + f"/tgif{i}.png")
        scope._shutdown_kaleido()  # temporary fix for memory leak

    vut.gif_from_pngs(tdir,
                      file_name,
                      n_frames,
                      png_fname="tgif",
                      add_zeros=False)