Skip to content

Instantly share code, notes, and snippets.

@samuelsmal
Created April 8, 2020 12:11
Show Gist options
  • Save samuelsmal/432db47096cbf5e141bff37d2f367ab1 to your computer and use it in GitHub Desktop.
Save samuelsmal/432db47096cbf5e141bff37d2f367ab1 to your computer and use it in GitHub Desktop.
python imageio creation of video from matplotlib figures
import warnings
import imageio
def gen_frames(data):
bandpower_over_time_index = data.index
frames = []
# loop over your images
for idx, time_index in enumerate(times_index_to_plot):
fig = plt.figure(figsize=(10, 10))
plt.plot(data)
plt.title("super cool title")
fig.canvas.draw()
mat = np.array(fig.canvas.renderer._renderer)
mat = cv2.cvtColor(mat, cv2.COLOR_RGB2BGR)
frames.append(mat)
plt.close()
return frames
def save_frames(frames, file_name, **kwargs):
"""uses imageio to save the given frames
does only store `mp4` videos, adapt `format`
frames: list of frames, each frame has to be a 3d array: [height, width, channels]
"""
_kwargs = {'fps': 1}
with warnings.catch_warnings():
warnings.filterwarnings('ignore', message='IMAGEIO FFMPEG_WRITER WARNING: input image is not divisible by macro_block_size=16')
imageio.mimsave(file_name, frames, format='mp4', **_kwargs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment