需要帮助
我正在使用“ moving_mnist”数据集。使用tfds.load("moving_mnist")
加载此数据,然后使用tfds.as_numpy()
将其转换为数组,这将返回形状为(20,64,64,1)的图像序列数组,其中20是帧数。现在,我想要在jupyter笔记本中将这些数组显示为GIF,请参见下面尝试的代码,但是它将为最后一帧生成简单图像。
import tensorflow_datasets as tfds
ds, ds_info = tfds.load("moving_mnist", with_info = True,split="test")
num_examples = 3
examples = list(dataset_utils.as_numpy(ds.take(num_examples)))
fig = plt.figure(figsize=(3*3, 3*3))
fig.subplots_adjust(hspace=1/3, wspace=1/3)
for i, ex in enumerate(examples):
video = ex["image-sequence"]
frame,height, width, c = video.shape
if c == 1:
video = video.reshape(video.shape[:3])
for i in range(0,frame):
ax.imshow(video[i,:,:], animated=True)
Here是我得到但想要作为GIF的结果
您可以使用库array2gif。
在此示例中摘自docs:
import numpy as np
from array2gif import write_gif
dataset = [
np.array([
[[255, 0, 0], [255, 0, 0]], # red intensities
[[0, 255, 0], [0, 255, 0]], # green intensities
[[0, 0, 255], [0, 0, 255]] # blue intensities
]),
np.array([
[[0, 0, 255], [0, 0, 255]],
[[0, 255, 0], [0, 255, 0]],
[[255, 0, 0], [255, 0, 0]]
])
]
write_gif(dataset, 'rgbbgr.gif', fps=5)