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
 Any ideas of doing so ?
Any ideas of doing so ?
            Asked
            
        
        
            Active
            
        
            Viewed 5,178 times
        
    11
            
            
         
    
    
        Mostafa Hussein
        
- 361
- 4
- 16
- 
                    https://stackoverflow.com/questions/30227466/combine-several-images-horizontally-with-python – Carlo 1585 Jul 25 '18 at 11:42
- 
                    Hello @Carlo1585 Yes exactly, I checked this and tried it. But it doesn't support GIF format, it gave me two JPG beside each other – Mostafa Hussein Jul 25 '18 at 11:47
- 
                    not sure about sorry, to be honest I never used imageio, but I'll try to investigate more. In any case I went to the imageio git project and I saw that there are still different issues with gif images so I advice you to have a look there. – Carlo 1585 Jul 25 '18 at 11:55
- 
                    No problem, thanks a lot. But apart from imageio, is there another way to do this task ? – Mostafa Hussein Jul 25 '18 at 11:56
- 
                    Can't you use this? https://stackoverflow.com/a/30932152/2836621 – Mark Setchell Jul 25 '18 at 12:04
- 
                    @MarkSetchell Unfortunately, I don't have rights or authorization to install Magick. I know that this can be done easily by Magick, ffmpeg and so, but that's why I am trying to use a python script – Mostafa Hussein Jul 25 '18 at 12:15
- 
                    How about this? https://gist.github.com/jonschoning/7216290 – Mark Setchell Jul 25 '18 at 12:37
- 
                    This is the library of reading, writing GIFs – Mostafa Hussein Jul 25 '18 at 15:11
1 Answers
4
            
            
        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.
 
    
    
        Ozan Kurt
        
- 3,731
- 4
- 18
- 32
 
    
    
        rponthieu dev
        
- 126
- 1
- 9
- 
                    1`imageio` cannot write transparency layers to GIFs, which is what bit me. Stay away if you need that! – xjcl Jan 14 '20 at 06:11