Skip to content

Instantly share code, notes, and snippets.

@Mukosame
Created June 15, 2020 04:19
Show Gist options
  • Save Mukosame/769588ea6da2c5eee396b60190cfd31b to your computer and use it in GitHub Desktop.
Save Mukosame/769588ea6da2c5eee396b60190cfd31b to your computer and use it in GitHub Desktop.
import os, glob
import imageio
from PIL import Image
def mkdir(path):
if not os.path.isdir(path):
os.mkdir(path)
def folder_to_quick_gif(infolder, outpath):
if not outpath.endswith('.gif'):
outpath = outpath + '.gif'
images = []
filenames = sorted(glob.glob(infolder + '/*'))
for filename in filenames:
images.append(imageio.imread(filename))
imageio.mimsave(outpath, images)
def folder_to_long_gif(infolder, outpath):
if not outpath.endswith('.gif'):
outpath = outpath + '.gif'
filenames = sorted(glob.glob(infolder + '/*'))
with imageio.get_writer(outpath, mode='I') as writer:
for filename in filenames:
image = imageio.imread(filename)
writer.append_data(image)
def folder_to_gif(infolder, outpath, duration=200, loop=0):
if not outpath.endswith('.gif'):
outpath = outpath + '.gif'
# https://pillow.readthedocs.io/en/stable/handbook/image-file-formats.html#gif
img, *imgs = [Image.open(f) for f in sorted(glob.glob(infolder + '/*'))]
img.save(fp=outpath, format='GIF', append_images=imgs,
save_all=True, duration=duration, loop=loop)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment