【问题标题】:combine several GIF horizontally - python水平组合几个GIF - python
【发布时间】:2019-01-02 04:50:01
【问题描述】:

我有两个 GIF 文件,我想将它们水平组合以显示在彼此旁边,并且它们一起播放。 它们具有相同的框架。 我在网上尝试了很多解决方案,但没有找到支持 GIF 的东西。我认为imageio 包支持 gif,但我找不到使用它将两个组合在一起的方法 简单地说,我想要这样的例子 有什么想法吗?

【问题讨论】:

  • 你好@Carlo1585 是的,我检查了这个并试过了。但是它不支持GIF格式,它给了我两个并排的JPG
  • 不确定抱歉,老实说我从未使用过 imageio,但我会尝试进行更多调查。无论如何,我去了 imageio git 项目,发现 gif 图像仍然存在不同的问题,所以我建议你看看那里。
  • 没问题,非常感谢。但除了 imageio,还有其他方法可以完成这项任务吗?
  • 你不能用这个吗? stackoverflow.com/a/30932152/2836621

标签: python image gif python-imageio


【解决方案1】:

我会编写这样的代码:

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()

所以魔术就是使用 hstack numpy 函数。它基本上会水平堆叠它们。这仅适用于两个 gif 的尺寸相同的情况。

【讨论】:

  • imageio 无法将透明层写入 GIF,这让我很苦恼。如果需要,请远离!
猜你喜欢
  • 2011-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
相关资源
最近更新 更多