5
4

More than 3 years have passed since last update.

Pythonでgif動画を作成

Posted at

初めに

いろいろあってgif動画の出力をする機会があったので,軽くまとめました.かなり短いです.

関数

import imageio

def make_gif(frames, filename, duration=1./60.):
    imageio.mimsave(filename, frames, 'GIF', **{'duration': duration})

引数は (高さ✕幅✕RGB) の画像 (uint8のnp.arrayとかを想定)の配列,出力ファイル名,gif動画のフレームレートです.gif動画の作成の実装はPIL.Imageを使うことが多いですが,自分の環境で何故かうまく行かなかったのでimageioで実装することになりました.

おわり

誰かの役に立てば幸いです.

どんな感じかみたい人用

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') 

出力はQiita上に貼れなかったのでないです.

5
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
4