Making GIFs in Python
If you’re searching for a quick way to make GIFs out of a folder full of images, the Pillow library in Python does the trick for you!
#imports
from PIL import Image
import os
#Change working directory to folder containing images to be GIF-fed
os.chdir("path_to_folder_with_images" )
#Ensure your images are in the order you want them to appear in the GIF
print("The images will be GIF-fed in the following order")
print(sorted(os.listdir())
#Open the images as a PIL image object and store them in a list
images = []
for img_file_name in sorted(os.listdir()):
img = Image.open(img_file_name)
images.append(img)
#Save images to an output.gif
images[0].save('output.gif',
save_all=True, append_images=images[1:],
optimize=False,
duration=800, loop=0)
Main parameters of Image.save()
-
loop : loop=0 makes the GIF loop infinitely. loop=n makes the GIF loop n times.
-
duration : time in milliseconds each image will be displayed for