Home > Back-end >  How do I connect two GIFs to play one after another in Python?
How do I connect two GIFs to play one after another in Python?

Time:08-24

If I have two GIFs, GIF 1 being 10 seconds long and GIF 2 being 5 seconds long, is there a way to connect them so the final GIF is a total of 15 seconds long?

Would I have to loop through each frame of both the GIFs with imageio.mimread() and output, once all the frames are read in memory?

Or is there another way by knowing the start and end times and shifting it?

CodePudding user response:

I think this works:

from pygifsicle import gifsicle

gifsicle(
    sources=["imageio_00.gif", "imageio_01.gif"], # or a single_file.gif
    destination="destination.gif", # or just omit it and will use the first source provided.
    optimize=True, # Whetever to add the optimize flag of not
    colors=256, # Number of colors t use
    options=["--verbose"] # Options to use.
)

CodePudding user response:

You already found a way to do it, but as you were referring to imageio let me add an answer using it. At least in the old imageio v2 API it was a pretty straightforward business too even if more verbose.

import imageio

gif_1 = imageio.get_reader(path_gif_1)
gif_2 = imageio.get_reader(path_gif_2)

combined_gif = imageio.get_writer('combined.gif')

for frame in gif_1:
    combined_gif.append_data(frame)

for frame in gif_2:
    combined_gif.append_data(frame)

gif_1.close()
gif_2.close()    
combined_gif.close()
  • Related