【问题标题】:How to make a movie out of a set of images stacked in a numpy array?如何从堆叠在 numpy 数组中的一组图像中制作电影?
【发布时间】:2018-11-15 04:46:55
【问题描述】:

我正在尝试用一组堆叠成第三维的 2d numpy 数组制作一部电影(或任何可以按顺序显示结果的东西)。

为了说明我在说什么,想象一个 9x3x3 numpy 数组,其中我们有 9 个不同的 3x3 数组的序列,如下所示:

import numpy as np
#creating an array where a position is occupied by
# 1 and the others are zero
a = [[[0,0,1],[0,0,0],[0,0,0]],[[0,1,0],[0,0,0],[0,0,0]], [[1,0,0],[0,0,0],[0,0,0]], [[0,0,0],[0,0,1],[0,0,0]], [[0,0,0],[0,1,0],[0,0,0]], [[0,0,0],[1,0,0],[0,0,0]], [[0,0,0],[0,0,0],[0,0,1]], [[0,0,0],[0,0,0],[0,1,0]], [[0,0,0],[0,0,0],[1,0,0]]]
a = np.array(a)

这样 a[0], ... a[n] 会返回如下内容:

In [10]: a[1]
Out[10]: 
array([[0, 1, 0],
       [0, 0, 0],
       [0, 0, 0]])

但是改变 1 的位置并用以下几行绘制一个简单的图:

img = plt.figure(figsize = (8,8))
    plt.imshow(a[0], origin = 'lower')
    plt.colorbar(shrink = 0.5)
    plt.show(img)

会给出输出:

那么创建电影的最合适的方法是什么,显示与上图相似的结果,每个结果都堆叠在“a”的第一个维度中,以便观察每个结果如何发生变化不同的步骤(帧)?

感谢您的关注和时间!

【问题讨论】:

  • 你的最后一句话不是很清楚,你是真的想为情节设置动画还是只是将所有帧堆叠成水平或垂直条带?
  • 我想为它制作动画。

标签: python arrays python-3.x numpy matplotlib


【解决方案1】:

你可以使用matplotlib animation api

这是一个基于this example的快速原型

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

a = [[[0,0,1],[0,0,0],[0,0,0]],
     [[0,1,0],[0,0,0],[0,0,0]],
     [[1,0,0],[0,0,0],[0,0,0]],
     [[0,0,0],[0,0,1],[0,0,0]],
     [[0,0,0],[0,1,0],[0,0,0]],
     [[0,0,0],[1,0,0],[0,0,0]],
     [[0,0,0],[0,0,0],[0,0,1]],
     [[0,0,0],[0,0,0],[0,1,0]],
     [[0,0,0],[0,0,0],[1,0,0]]]
a = np.array(a)

fig, ax = plt.subplots(figsize=(4, 4))

frame = 0
im = plt.imshow(a[frame], origin='lower')
plt.colorbar(shrink=0.5)

def update(*args):
    global frame

    im.set_array(a[frame])

    frame += 1
    frame %= len(a)

    return im,

ani = animation.FuncAnimation(fig, update, interval=500)
plt.show()

您也可以使用ImageMagickFileWriter 将它们保存为 gif,只需将最后两行替换为

ani = animation.FuncAnimation(fig, update, len(a))
writer = animation.ImageMagickFileWriter(fps=2)
ani.save('movie.gif', writer=writer) 

【讨论】:

    【解决方案2】:

    您可以使用 imageio 从输出创建 gif:

    import numpy as np
    import matplotlib.pyplot as plt
    import imageio
    
    a = [[[0,0,1],[0,0,0],[0,0,0]], 
         [[0,1,0],[0,0,0],[0,0,0]], 
         [[1,0,0],[0,0,0],[0,0,0]], 
         [[0,0,0],[0,0,1],[0,0,0]], 
         [[0,0,0],[0,1,0],[0,0,0]], 
         [[0,0,0],[1,0,0],[0,0,0]], 
         [[0,0,0],[0,0,0],[0,0,1]], 
         [[0,0,0],[0,0,0],[0,1,0]], 
         [[0,0,0],[0,0,0],[1,0,0]]]
    
    a = np.array(a)
    
    images = []
    
    for array_ in a:
        file_path = "C:\file\path\image.png"
    
        img = plt.figure(figsize = (8,8))
        plt.imshow(array_, origin = 'lower')
        plt.colorbar(shrink = 0.5)
    
        plt.savefig(file_path) #Saves each figure as an image
        images.append(imageio.imread(file_path)) #Adds images to list
        plt.clf()
    
    plt.close()
    imageio.mimsave(file_path + ".gif", images, fps=1) #Creates gif out of list of images
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-05
      相关资源
      最近更新 更多