Python - create gif from numerically-labeled individual frames

48 views Asked by At

I have a directory that contains PNGs named frame1.png, frame2.png, and so on. I want to create a GIF using these PNGs as frames, in the order However my code creates a GIF using frame1.png as the first frame and frame10.png as the second frame. This is probably due to my code reading every PNG in the directory and combining each frame all at once. How do I generate a GIF using frame1.png as the first frame and frame2.png as the second frame, and so on?

My code is:

import glob
from PIL import Image
def make_gif(frame_folder):
    frames = [Image.open(image) for image in glob.glob(f"{frame_folder}/*.png")]
    frame_one = frames[0]
    frame_one.save("gif_name.gif", format="GIF", append_images=frames,
               save_all=True, duration=100, loop=0)
    
if __name__ == "__main__":
    make_gif("path_to_directory")

Specifically, how do I ensure that glob sorts the files the way I want? Should I use another package?

I have tried sorting the files with the os package but couldn't figure out what to do next.

0

There are 0 answers