def implay(array): try: if array.dtype != np.uint8: array = (array * 255).astype(np.uint8) def make_frame(t): return array[..., round(t * 30.0)] clip = VideoClip(make_frame, duration=array.shape[-1] / 30.0) clip.preview(fps=30) #_ = raw_input("quit...") finally: import pygame.display pygame.display.quit()
def test1(): import matplotlib.pyplot as plt import numpy as np from moviepy.editor import VideoClip from moviepy.video.io.bindings import mplfig_to_npimage x = np.linspace(-2, 2, 200) duration = 2 fig, ax = plt.subplots() def make_frame(t): ax.clear() ax.plot(x, np.sinc(x**2) + np.sin(x + 2 * np.pi / duration * t), lw=3) ax.set_ylim(-1.5, 2.5) return mplfig_to_npimage(fig) animation = VideoClip(make_frame, duration=duration) # animation.write_gif('matplotlib.gif', fps=20) animation.preview()
def show_animation( self, a_function_which_returns_an_image_according_to_a_time_variable, duration=3, fps=24, saving_path=None): """ the function looks like `func(t)`. t is a float variable in seconds, for example, 1.2 = 1 second + 0.2 second """ def wrap_function(t): array = a_function_which_returns_an_image_according_to_a_time_variable( t) assert isinstance( array, np.ndarray), "this function should return an numpy array" if array.shape[2] == 4: array = array[:, :, 0:3] return array animation = VideoClip(wrap_function, duration=duration) if self._notebook: result = animation.ipython_display(fps=fps, loop=True, autoplay=True) self._IPython.display.display(result) else: animation.preview(fps=fps) if saving_path != None: if len(saving_path.split(".")) > 0: extension = saving_path.split(".")[-1] if extension.lower() == "gif": animation.write_gif(saving_path, fps=fps) elif extension.lower() == "mp4": animation.write_videofile(saving_path, fps=fps) else: print("you can only save gif or mp4!") exit()