2016-01-24 44 views
2

我有幾個圖像顯示了時間的變化。我想象他們在同一地塊的許多圖像與下面的代碼:如何從matplotlib中的圖像數組製作視頻?

import matplotlib.pyplot as plt 
import matplotlib.cm as cm 

img = [] # some array of images 
fig = plt.figure() 
for i in xrange(6): 
    fig.add_subplot(2, 3, i + 1) 
    plt.imshow(img[i], cmap=cm.Greys_r) 

plt.show() 

,並得到類似:enter image description here

這是確定的,但我寧願動畫他們得到something like this video。我怎樣才能達到這與python,最好(不一定)與matplotlib

回答

2

對於未來的自己,這裏是我結束了:

def generate_video(img): 
    for i in xrange(len(img)): 
     plt.imshow(img[i], cmap=cm.Greys_r) 
     plt.savefig(folder + "/file%02d.png" % i) 

    os.chdir("your_folder") 
    subprocess.call([ 
     'ffmpeg', '-framerate', '8', '-i', 'file%02d.png', '-r', '30', '-pix_fmt', 'yuv420p', 
     'video_name.mp4' 
    ]) 
    for file_name in glob.glob("*.png"): 
     os.remove(file_name) 
0

我實現了,只是適合你及新加入的一個方便的腳本。試試吧here

對於示例:

imagelist = YOUR-IMAGE-LIST 
def redraw_fn(f, axes): 
    img = imagelist[f] 
    if not redraw_fn.initialized: 
     redraw_fn.im = axes.imshow(img, animated=True) 
     redraw_fn.initialized = True 
    else: 
     redraw_fn.im.set_array(img) 
redraw_fn.initialized = False 

videofig(len(imagelist), redraw_fn, play_fps=30)