GIF,Python-并排

时间:2019-12-09 18:49:31

标签: python numpy matplotlib animation gif

我有两个列表

globalCache - [4000 of 2D Numpy Array (200,200) size].
globalCache1 - [4000 of 2D Numpy Array (200,200) size].

每个缓存中都有4000个由零和一组成的2D NumPy数组。

到目前为止,我正在尝试创建4000张并排放置的图像的动画GIF:

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

class AnimatedGif:
    def __init__(self, size=(680, 520)):
        self.fig, self.fig_axes = plt.subplots(1,2)

        self.fig.set_size_inches(size[0] / 50, size[1] / 100)

        self.fig_axes[0].set_xticks([])
        self.fig_axes[0].set_yticks([])

        self.fig_axes[1].set_xticks([])
        self.fig_axes[1].set_yticks([])

        self.images = []

    def add(self, image1,image2, label='', _stck1=-999, _stck2=-999):

       self.fig_axes[0].imshow(image1, vmin=0, cmap = 'copper', vmax=1, animated=True)
       self.fig_axes[1].imshow(image2, vmin=0, cmap = 'copper', vmax=1, animated=True)

       lbl = int(int(label)/100 * 100)
       if lbl == 0:
           lbl = ''
       self.fig_axes[0].text(-30,10, lbl, fontsize=15, color='red')
       self.fig_axes[0].text(200, 10, 'Stickiness : {0}'.format(_stck1),fontsize=12, color='red')

       self.fig_axes[1].text(-30,10, lbl, fontsize=15, color='red')
       self.fig_axes[1].text(200, 10, 'Stickiness : {0}'.format(_stck2),fontsize=12, color='red')
       self.artists.append(self.fig_axes[0].get_children())
       self.artists.append(self.fig_axes[1].get_children())

    def save(self, filename):
       animation = anim.ArtistAnimation(self.fig, self.artists)
       animation.save(filename, writer='imagemagick', fps=50)

但是当我使用

animated_gif = AnimatedGif()

for i in range(len(globalCache)):

    animated_gif.add(globalCache1[i], 
                     globalCache[i], 
                     label=str(i), 
                     _stck1 = '1', _stck2 = '0.1')
    if i == 100 : break

animated_gif.save('ouput.gif')

我得到以下输出:

enter image description here

预期的输出是这样的(单个GIF使用Python创建,然后使用Online工具并排放置)

enter image description here

有什么想法可能会出错吗?

0 个答案:

没有答案