14

I'm trying to generate an animated GIF using images2gif.py (pastebin to the most recent verson : bit.ly/XMMn5h ).

I'm using this Python script:

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

However, I am getting the following error:

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

It seems to me that occur is empty. What is wrong, and is there a better way?

8
  • 1
    Are the gif frames correct? Don't they miss the palette chunk somehow?
    – 9000
    Commented Oct 24, 2012 at 1:54
  • The GIFs are a series of consecutive numbered GIF files in the same directory as the above code. I can provide a sample if that will help.
    – Harry
    Commented Oct 24, 2012 at 2:02
  • Seeing a couple of frames would be helpful. Please make them accessible somewhere.
    – 9000
    Commented Oct 24, 2012 at 2:09
  • Here's a zip of the folder that contains the entire series of images.[link] (docs.google.com/open?id=0B18Gp-SPOX2wU0Z1S3RKX3RzS2M)
    – Harry
    Commented Oct 24, 2012 at 2:26
  • 1
    How can file_names be a list of .png files, given the filter fn.endswith('.gif')?
    – nneonneo
    Commented Oct 24, 2012 at 5:46

3 Answers 3

5

OK I have tested your exact code on two different machines, and it works perfectly on both. One machine is Ubuntu 12.04 and the other is running Windows XP. They are both using Python 2.7, and the latest version of images2gif which I downloaded from here. I recommend the following:

  1. check what version of python and the libraries you are using, try and get the latest ones.
  2. test it on another machine
  3. try and uninstall python and all the libraries and try and re-install
1
  • it worked.. using the images2gif.py from the link you list did the trick.
    – Harry
    Commented Oct 25, 2012 at 7:51
4

Python, Create a .gif from an numpy ndarray of numpy ndarrays representing images :

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)

Prints:

[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
  • 1
    Note that you'll probably have to do the added step of import imageio imageio.plugins.ffmpeg.download() the first time you try running this to get around github.com/Zulko/moviepy/issues/493
    – lanery
    Commented Nov 10, 2017 at 9:36
  • @lanery is right, but it is not as convoluted to fix this issue as it once was. Install ffmpeg with pip install imageio-ffmpeg Commented Jan 21, 2021 at 14:14
0

In the list constructor

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

the endswith is case sensitive, so if you happen to have all GIF images, then they won't be found, and you will get a

    ValueError: max() arg is an empty sequence

error.

I suggest using

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

for success with this. Also, it's a good idea to create the animated gif file in the parent (or at least another) directory.

Not the answer you're looking for? Browse other questions tagged or ask your own question.