Skip to content

Instantly share code, notes, and snippets.

@hiwonjoon
Last active August 4, 2019 16:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hiwonjoon/eba0222f505dd766fefacf977bb28cc3 to your computer and use it in GitHub Desktop.
Save hiwonjoon/eba0222f505dd766fefacf977bb28cc3 to your computer and use it in GitHub Desktop.
Code Snippets for displaying image frames (NWHC format, numpy array) in Jupyter Notebook
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation, rc
from IPython.core.display import display, HTML
%matplotlib inline
%load_ext autoreload
%autoreload 2
def display_frames_as_gif(frames):
fig, ax = plt.subplots()
patch = ax.imshow(frames[0])
title_obj = ax.set_title(0)
def animate(i):
patch.set_data(frames[i])
plt.setp(title_obj,color='k', text=('Time: %d' % (i)))
anim = animation.FuncAnimation(fig, animate, range(len(frames)), interval=50)
display(HTML(anim.to_html5_video()))
plt.close()
import moviepy.editor as mpy
clip = mpy.ImageSequenceClip([(ob*255.).astype(np.uint8) for ob in obs],fps=30)
clip.write_videofile('video.mp4', verbose=False,ffmpeg_params=['-y'],progress_bar=False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment