I had a chance to output a gif video due to various reasons, so I summarized it lightly. It's pretty short.
import imageio
def make_gif(frames, filename, duration=1./60.):
imageio.mimsave(filename, frames, 'GIF', **{'duration': duration})
The arguments are the array of the image (height ✕ width ✕ RGB) (assuming uint8 np.array), the output file name, and the frame rate of the gif video. I often use PIL.Image to implement gif video creation, but for some reason it didn't work in my environment, so I decided to implement it with imageio.
I hope it helps someone.
import imageio
import numpy as np
def make_gif(frames, filename, duration=1./30.):
imageio.mimsave(filename, frames, 'GIF', **{'duration': duration})
if __name__ == "__main__":
import gym
frame = []
env = gym.make('Seaquest-v0')
obs = env.reset()
for _ in range(500):
obs, _, _, _ = env.step(env.action_space.sample())
frame.append(obs)
make_gif(frame, 'out.gif')
It's not that the output couldn't be pasted on Qiita.
Recommended Posts