gpt4 book ai didi

python - 如何使用数组制作 GIF

转载 作者:行者123 更新时间:2023-12-05 05:04:42 25 4
gpt4 key购买 nike

需要一些帮助

我正在使用“moving_mnist”数据集。使用 tfds.load("moving_mnist") 加载此数据,然后使用 tfds.as_numpy() 将其转换为数组,这将返回形状为 (20,64) 的图像序列数组,64,1) 其中 20 是帧数。现在我想要的是,在我的 jupyter notebook 中将这些数组显示为 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

最佳答案

moviepy图书馆使这很容易:

import numpy as np
frames = np.random.randint(256, size=[20, 64, 64, 1], dtype=np.uint8) # YOUR DATA HERE

# save it as a gif
from moviepy.editor import ImageSequenceClip
clip = ImageSequenceClip(list(frames), fps=20)
clip.write_gif('test.gif', fps=20)

然后,如果您想在 jupyter 笔记本中显示该 gif,您可以在下一个单元格中键入:

from IPython.display import display, Image
Image('test.gif')

关于python - 如何使用数组制作 GIF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60914488/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com