在Python中生成动画GIF

时间:2012-10-24 01:50:24

标签: python gif animated-gif

我正在尝试使用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

在我看来,发生的是空的。有什么不对,还有更好的办法吗?

3 个答案:

答案 0 :(得分:5)

好的,我已经在两台不同的机器上测试了您的确切代码,并且它在两者上都运行良好。一台机器是Ubuntu 12.04,另一台机器运行Windows XP。他们都使用Python 2.7,以及I downloaded from here的最新版本的images2gif。我推荐以下内容:

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

答案 1 :(得分:1)

Python,从表示图像的numpy ndarray的numpy ndarray创建.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]

答案 2 :(得分: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文件是个好主意。