使用 images2gif 生成 gif

generate gif with images2gif

我正在从一些 .png 图片生成带有 images2gif 的 gif。我正在使用以下脚本:

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

但是我有以下错误:

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

cosa.png 是我要用来创建 gif 的图片之一。问题似乎出在:

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

但是我检测不到

open(filename) 在当前工作目录中查找,如果 filename 不是 绝对路径。当前工作目录是脚本所在的目录 运行.

要么使用 os.chdir 更改工作目录,要么将所有文件名设为绝对文件名:

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