python create gif code example

Example 1: make gif from images in python

from PIL import Image
import glob
 
# Create the frames
frames = []
imgs = glob.glob("*.png")
for i in imgs:
    new_frame = Image.open(i)
    frames.append(new_frame)
 
# Save into a GIF file that loops forever
frames[0].save('png_to_gif.gif', format='GIF',
               append_images=frames[1:],
               save_all=True,
               duration=300, loop=0)

Example 2: python create gif

from moviepy.editor import *

clip = (VideoFileClip("video.mp4")
        .subclip((1,22.65),(1,23.2))
        .resize(0.3))
clip.write_gif("video.gif")

Example 3: generate gif py

import imageio
images = []
for filename in filenames:
    images.append(imageio.imread(filename))
imageio.mimsave('/path/to/movie.gif', images)

Example 4: generate gif py

import imageio
with imageio.get_writer('/path/to/movie.gif', mode='I') as writer:
    for filename in filenames:
        image = imageio.imread(filename)
        writer.append_data(image)