2015-09-05 76 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