Create gif video in Python

at first

I had a chance to output a gif video due to various reasons, so I summarized it lightly. It's pretty short.

function

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.

end

I hope it helps someone.

For those who want to feel like

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

Create gif video in Python
Create SpatiaLite in Python
Create a GIF file using Pillow in Python
Create a function in Python
Create a dictionary in Python
Create 3d gif with python3
Create a binary file in Python
Create Gmail in Python without API
Create Python project documentation in Sphinx
Create a Kubernetes Operator in Python
Create a random string in Python
Create and read messagepacks in Python
Quadtree in Python --2
Python in optimization
CURL in python
Metaprogramming in Python
Python 3.3 in Anaconda
Geocoding in python
SendKeys in Python
Make ASCII art GIF animation in Python
Meta-analysis in Python
Create ScriptableObject in Python when building ADX2
Unittest in python
[LLDB] Create your own command in Python
Create a simple GUI app in Python
Create a JSON object mapper in Python
Epoch in Python
Discord in Python
Create Qt designer parts in Python (PyQt)
Sudoku in Python
DCI in Python
quicksort in python
nCr in python
N-Gram in Python
Programming in python
Plink in Python
Constant in python
Lifegame in Python.
FizzBuzz in Python
Sqlite in python
StepAIC in Python
[GPS] Create a kml file in Python
N-gram in python
LINE-Bot [0] in Python
Csv in python
Disassemble in Python
Reflection in Python
Constant in python
nCr in Python.
format in python
Scons in Python3
Puyo Puyo in python
python in virtualenv
PPAP in Python
Quad-tree in Python
Reflection in Python
Chemistry in Python
Hashable in python
DirectLiNGAM in Python
LiNGAM in Python
Flatten in python