Created
April 8, 2020 12:11
-
-
Save samuelsmal/432db47096cbf5e141bff37d2f367ab1 to your computer and use it in GitHub Desktop.
python imageio creation of video from matplotlib figures
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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