[英]How Can I make GIF using arrays
Need some help需要一些帮助
I am working with " moving_mnist " dataset.我正在使用“ moving_mnist ”数据集。 Loading this data using
tfds.load("moving_mnist")
and then convert it in arrays using tfds.as_numpy()
which will return image sequence arrays of shape (20,64,64,1) where 20 is number of frames.使用
tfds.load("moving_mnist")
加载此数据,然后使用tfds.as_numpy()
将其转换为数组,这将返回形状为 (20,64,64,1) 的图像序列数组,其中 20 是帧数。 Now what I want, to show these arrays as GIF in my jupyter notebook please see below code which I tried but it will generate simple image for last frame.现在我想要的是,要在我的 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)
The moviepy library makes this pretty easy: 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)
Then if you want to show that gif in a jupyter notebook, in the next cell you can type:然后,如果您想在 jupyter notebook 中显示该 gif,则可以在下一个单元格中键入:
from IPython.display import display, Image
Image('test.gif')
you could use the library array2gif.你可以使用库array2gif。
Here is in example taken from the 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.