使用Python生成动态GIF

14

我正在尝试使用images2gif.py生成动画GIF(最新版本的pastebin链接为:bit.ly/XMMn5h)。

我正在使用这个Python脚本:

__author__ = 'Robert'
from images2gif import writeGif
from PIL import Image
import os

file_names = sorted((fn for fn in os.listdir('.') if fn.endswith('.gif')))
#['animationframa.png', 'animationframb.png', ...] "

images = [Image.open(fn) for fn in file_names]

size = (150,150)
for im in images:
    im.thumbnail(size, Image.ANTIALIAS)

print writeGif.__doc__

filename = "my_gif.GIF"
writeGif(filename, images, duration=0.2)

然而,我遇到了以下错误:

File "C:\Python27\lib\images2gif.py" , line 418, in writeGifToFile
globalPalette = palettes[ occur.index(max(occur)) ] ValueError: max() 
arg is an empty sequence

我觉得出现的列表是空的。有什么问题吗?有更好的方法吗?


1
GIF帧是否正确?它们不会错过调色板块吗? - 9000
看到一些帧会很有帮助,请将它们放在可访问的地方。 - 9000
这是一个包含整个图像系列的文件夹的压缩包。[链接] (https://docs.google.com/open?id=0B18Gp-SPOX2wU0Z1S3RKX3RzS2M) - Harry
1
考虑到筛选器 fn.endswith('.gif')file_names 怎么可能是一个 .png 文件列表呢? - nneonneo
你好。我已经创建了最终的GIF,但它是动画的?我该如何打开? - Bárbara Duarte
显示剩余3条评论
3个回答

5

好的,我已经在两台不同的机器上测试了您的代码,并且它们在两台机器上都完美地运行。一台机器是Ubuntu 12.04,另一台机器运行Windows XP。它们都使用Python 2.7和最新版本的images2gif,我从这里下载。我建议您执行以下操作:

  1. 检查您正在使用的Python和库的版本,尝试获取最新版本。
  2. 在另一台计算机上进行测试
  3. 尝试卸载Python和所有库,并尝试重新安装

它起作用了。使用你列出的链接中的images2gif.py就解决了问题。 - Harry

4

Python,从表示图像的numpy ndarrays的numpy ndarrays创建.gif:

import os
import numpy as np
from moviepy.editor import ImageSequenceClip
#Installation instructions: 
#    pip install numpy
#    pip install moviepy
#    Moviepy needs ffmpeg tools on your system
#        (I got mine with opencv2 installed with ffmpeg support)

def create_gif(filename, array, fps=10, scale=1.0):
    """creates a gif given a stack of ndarray using moviepy
    Parameters
    ----------
    filename : string
        The filename of the gif to write to
    array : array_like
        A numpy array that contains a sequence of images
    fps : int
        frames per second (default: 10)
    scale : float
        how much to rescale each image by (default: 1.0)
    """
    fname, _ = os.path.splitext(filename)   #split the extension by last period
    filename = fname + '.gif'               #ensure the .gif extension
    if array.ndim == 3:                     #If number of dimensions are 3, 
        array = array[..., np.newaxis] * np.ones(3)   #copy into the color 
                                                      #dimension if images are 
                                                      #black and white
    clip = ImageSequenceClip(list(array), fps=fps).resize(scale)
    clip.write_gif(filename, fps=fps)
    return clip

randomimage = np.random.randn(100, 64, 64)       
create_gif('test.gif', randomimage)                 #example 1

myimage = np.ones(shape=(300, 200))
myimage[:] = 25
myimage2 = np.ones(shape=(300, 200))
myimage2[:] = 85
arrayOfNdarray = np.array([myimage, myimage2])

create_gif(filename="grey_then_black.gif",          #example 2
           array=arrayOfNdarray, 
           fps=5, 
           scale=1.3)

输出:

[MoviePy] Building file test.gif with imageio
100%|██████████████████████████████████████████| 100/100 [00:00<00:00, 905.27it/s]

[MoviePy] Building file grey_then_black.gif with imageio
 67%|█████████████████████████▎                | 2/3 [00:00<00:00, 65.65it/s]

1
请注意,您可能需要执行额外的步骤 import imageioimageio.plugins.ffmpeg.download(),以便在第一次尝试运行此代码时解决 https://github.com/Zulko/moviepy/issues/493 问题。 - lanery
@lanery是正确的,但解决这个问题并不像以前那样复杂。使用pip install imageio-ffmpeg安装ffmpeg即可。 - Can H. Tartanoglu

0
在列表构造器中
    (fn for fn in os.listdir('.') if fn.endswith('.gif'))

endswith是区分大小写的,所以如果你恰好有所有的GIF图像,那么它们将无法被找到,你会得到一个

    ValueError: max() arg is an empty sequence

错误。

我建议使用

    (fn for fn in os.listdir('.') if fn.endswith('.gif') or fn.endswith('.GIF'))

为了成功,请按照这个步骤。同时,在父目录(或至少另一个)中创建动画gif文件是一个好主意。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接