Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

combine several GIF horizontally - python

I have two GIF files, and I want to combine them horizontally to be shown beside each other, and they play together. They have equal frames. I tried a lot online for a solution, but didn't find something which is supporting GIF. I think the imageio package supports gif, but I can't find a way to use it to combine two together Simply, I want something like this example enter image description here Any ideas of doing so ?

like image 558
Mostafa Hussein Avatar asked Jul 25 '18 11:07

Mostafa Hussein


People also ask

How to join two animated GIFs side by side?

You can select multiple GIF images Max total size: 100MB Output image: Online tool for joining two animated GIFs side by side Upload multiple GIFs, set the position and this tool will join them together one after another. The images should have similar sizes and frame rates. You can upload animated images in GIF and WebP formats.

What are GIFs in Python?

/ Pillow, Python Animated GIFs are an image type that contains multiple images with slight differences. These are then played back kind of like a cartoon is. You could even think of it as a flip-book with a stick man that is slightly different on each page.

How do I make a gif from a list of images?

Once you have your Python list of images, you tell Pillow to save () it as a GIF using the first Image in your Python list. To make that happen, you need to specifically tell Pillow that the format is set to "GIF". You also pass in your frames of animation to the append_images parameter.

How to create animated GIFs with pillow?

You can create your own animated GIFs using the Python programming language and the Pillow package. Let's get started! You will need to have Python installed on your machine. You can install it from the Python website or use Anaconda.


1 Answers

I would code something like this :

import imageio
import numpy as np    

#Create reader object for the gif
gif1 = imageio.get_reader('file1.gif')
gif2 = imageio.get_reader('file2.gif')

#If they don't have the same number of frame take the shorter
number_of_frames = min(gif1.get_length(), gif2.get_length()) 

#Create writer object
new_gif = imageio.get_writer('output.gif')

for frame_number in range(number_of_frames):
    img1 = gif1.get_next_data()
    img2 = gif2.get_next_data()
    #here is the magic
    new_image = np.hstack((img1, img2))
    new_gif.append_data(new_image)

gif1.close()
gif2.close()    
new_gif.close()

So the magic trick is to use the hstack numpy function. It will basically stack them horizontally. This only work if the two gif are the same dimensions.

like image 195
rponthieu dev Avatar answered Sep 18 '22 17:09

rponthieu dev