如何用字典中的numpy数组表示的图像制作动画

时间:2018-04-19 13:36:53

标签: python python-3.x matplotlib

我目前正在使用numpy处理一些图像,我将新结果存储在字典中。下面是一个图像的例子:

enter image description here

一旦我有了带有结果的字典,也就是图像,我怎样才能制作出它们的电影序列?使用这个我打算观察结构如何随时间演变。

我尝试实现这一目标如下:

import matplotlib.pyplot as plt

def init(img):
    '''
    This function supports the ''make_anim'' function by plotting the
    background of each frame
    '''
    img.set_data([],[])

    return(img)


def updatefig(data, keys, i):
    '''
    This function supports the ''make_anim'' function by refreshing the 
    image space.
    '''

    img.set_data(data[keys[i]])

    return(img)

def make_anim(data, keys = []):
    '''
    This function will produce an animation from a dataset.
    '''
    if keys == []:
        keys = list(data.keys())

    dims = data[keys[0]].shape

    #defining the figure
    fig = plt.figure()

    #defining axis
    ax = plt.axes(xlim=(0, dims[1]), ylim=(0, dims[0]))

    #inserting the dataset to an image
    img = plt.imgshow(data, animated = True)
    #using the animation function
    ani = animation.FuncAnimation(fig, updatefig(data, keys), 
                                  interval = 50,
                                  frames = len(keys), blit = False)
    plt.show()

    return()

我很难找到处理类似问题的教程,当我找到它们时,我无法真正重现。

感谢您提前的时间!

安德烈

0 个答案:

没有答案