2015-09-05 75 views
0

我正在研究一个简单的脚本来下载一系列雷达图像,为它们添加覆盖图,然后将它们变成动画gif。 我使用visvis软件包中的Pillow和images2gif。 我之前问过关于堆叠透明gif文件的问题。 我设法得到这个工作,但images2gif是抛出一个错误。 这里是我的项目的相关代码:images2gif抛出“无法获取调色板”错误

from PIL import Image 
from visvis.vvmovie.images2gif import writeGif as gif 

background = Image.open("%IMAGEDIR%\Background.png") 
overlay = Image.open("%IMAGEDIR%\Overlay.png") 
frames = [] 
for i in range (10, 0, -1): 
    imagenum = ("%02d" % i) 
    radar = Image.open("%IMAGEDIR%\" + imagenum + ".gif") 
    radar = radar.convert("RGBA") 
    background.paste(radar, (0,0), radar) 
    background.paste(overlay, (0,0), overlay) 
    background.convert("RGB").convert("P", palette = Image.ADAPTIVE) 
    frames.append(background) 
print ("Writing image!") 
gif("%IMAGEDIR%\animation.gif", frames) 
print ("Done!") 

然而,这将引发一个错误,从images2gif:

Traceback (most recent call last): 
    File "%IMAGEDIR%\images.py", line 62, in <module> 
    gif("%IMAGEDIR%\animation.gif", frames) 
    File "C:\Python34\lib\site-packages\visvis\vvmovie\images2gif.py", line 578, in writeGif 
gifWriter.writeGifToFile(fp, images, duration, loops, xy, dispose) 
    File "C:\Python34\lib\site-packages\visvis\vvmovie\images2gif.py", line 418, in writeGifToFile 
    raise RuntimeError('Cannot get palette. ' 
RuntimeError: Cannot get palette. Maybe you should try imageio instead. 

要在images2gif相关线路,我觉得这一点:

# Obtain palette for all images and count each occurance 
palettes, occur = [], [] 
for im in images: 
    palette = getheader(im)[1] 
    if not palette: 
     palette = PIL.ImagePalette.ImageColor 
     if isinstance(palette, type(os)): 
      # Older or newer? version of Pil(low) 
      data = PIL.ImagePalette.ImagePalette().getdata() 
      palette = data[0].encode('utf-8') + data[1] 
      # Arg this does not work. Go use imageio 
      raise RuntimeError('Cannot get palette. ' 
           'Maybe you should try imageio instead.') 
    palettes.append(palette) 
for palette in palettes: 
    occur.append(palettes.count(palette)) 

我从来没有使用过imageio,而我已经在使用应该与images2gif一起工作的PIL。理想情况下,我不想将图像保存到磁盘 - 它们是小文件,但我想尽可能地使它工作。

非常感谢您的帮助!

注:我在numpy 1.9.2和visvis 1.9.2上使用Windows 10上的Python 3.4来测试它,但是这个脚本将在PythonAnywere上使用。

回答

4

试试这个版本,与我一起工作(与枕头2.9):https://github.com/rec/echomesh/blob/master/code/python/external/images2gif.py

+0

非常感谢这个建议。至少现在看起来,这个混乱的唯一地方就是节省了这个巨大改进的gif。它尝试保存时抛出以下错误:'TypeError:'str'不支持缓冲区接口'它将这个错误引发到image2gif.py的第445行,它是'fp.write(encoded(header)) ' – pogrmman

+0

好吧,我将保存例程中的'fp.write(encode(SOMETHING))'行改为'fp.write(SOMETHING.encode())',这消除了上述错误。该程序现在运行,但是当我尝试在GIMP中打开gif时,出现“代码大小超出范围”错误。所以,它好像是在保存某些东西,但不是一个有效的gif。现在我对如何解决这个问题感到非常困惑。 – pogrmman