Search code examples
pythongifanimated-gif

generate gif with images2gif


I am generating a gif with images2gif from some .png pictures. I am using the following script:

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

file_names = sorted((fn for fn in os.listdir('/home/manager/Desktop/sf_linux_shared/project/prueba') if fn.endswith('.png')))
#['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 have the following error:

IOError: [Errno 2] No such file or directory: 'cosa.png'

cosa.png is one of the pictures I want to create the gif with. The problem seems to be in:

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

but I cannot detect it


Solution

  • open(filename) looks in the current working directory if filename is not an absolute path. The current working directory is the directory from which the script is run.

    Either use os.chdir to change the working directory, or make all your filenames absolute:

    WORKDIR = '/home/manager/Desktop/sf_linux_shared/project/prueba'
    file_names = sorted((os.path.join(WORDIR, fn) 
                         for fn in os.listdir(WORKDIR) if fn.endswith('.png')))