GifWriter

class hcipy.plotting.GifWriter(filename, framerate=15, cleanup=True)

Bases: object

A writer of gif files from Matplotlib figures.

This function writes out individual frames to a filename.frames directory. When the close() function is called, or the object is removed by the garbage collector, the frames are collected in a single gif file. The individual frames are then deleted, if cleanup is True (default).

Parameters
framerateinteger

The number of frames per second of the generated gif file.

cleanupboolean

Whether to clean up the generated frames.

Methods Summary

add_frame(self[, fig, data, cmap, dpi])

Add a frame to the animation.

close(self)

Close the animation, create the final gif file and (potentially) remove the individual frames.

convert(self)

Convert all images in the frames directory into a gif file.

convert_to_gif(dest_filename, src_file_path, …)

Helper function to convert all files in a directory to a gif file.

Methods Documentation

add_frame(self, fig=None, data=None, cmap=None, dpi=None)

Add a frame to the animation.

Parameters
figMatplotlib figure

The Matplotlib figure acting as the animation frame.

datandarray

The image data array acting as the animation frame.

cmapMatplotlib colormap

The optional colormap for the image data.

dpiinteger or None

The number of dots per inch with which to save the matplotlib figure. If it is not given, the default Matplotlib dpi will be used.

Raises
RuntimeError

If the function was called on a closed GifWriter.

close(self)

Close the animation, create the final gif file and (potentially) remove the individual frames.

convert(self)

Convert all images in the frames directory into a gif file.

This function doesn’t remove the frames after conversion.

static convert_to_gif(dest_filename, src_file_path, framerate, src_file_suffix='png', num_files_to_convert=None)

Helper function to convert all files in a directory to a gif file.

Parameters
dest_filenamestring

The filename for the gif file.

src_file_pathstring

The path to the directory with all the frames as image files.

framerateinteger

The number of frames per second for the generated gif file.

src_file_suffixstring

The file extension of the image files.

num_files_to_convertinteger or None

How many frames are expected in the directory. If None, then no check will be done.

Raises
OSError

If the number of files in the directory differs from the number of expected files.