我正在尝试在matplotlib中创建动画,我看到了compression artifacts。当animation shows compression artifacts显示时,static image显示平滑的颜色连续体。如何在没有这些压缩工件的情况下保存动画?我从this answer获取了一些writer参数,但它们并没有解决问题。
您可以在this Google Colab notebook中运行代码,也可以在此处查看:
import numpy as np
from matplotlib import pyplot as plt
from matplotlib import animation
images = np.array([
np.tile(np.linspace(0, 1, 500), (50, 1)),
np.tile(np.linspace(1, 0, 500), (50, 1)),
])
fps = 1
fig = plt.figure(frameon=False)
ax = plt.Axes(fig, [0., 0., 1., 1.])
fig.add_axes(ax)
artists = [[ax.imshow(image, animated=True, cmap='jet')] for image in images]
anim = animation.ArtistAnimation(fig, artists, interval=1000/fps, repeat_delay=1000)
writer = animation.PillowWriter(fps=fps, bitrate=500, codec="libx264", extra_args=['-pix_fmt', 'yuv420p'])
anim.save('./test_animation.gif', writer=writer)
ax.imshow(images[0], animated=True, cmap='jet');谢谢你的建议!
发布于 2021-03-08 13:15:02
我找到了一个解决方案,可以在Colab中生成一个没有工件的gif (这在一定程度上要归功于@JohanC的评论)。
首先,我需要使用FFMpeg将动画保存为mp4视频。这将创建没有压缩伪影的高质量视频。
writer = animation.FFMpegWriter(fps=fps)
anim.save('./test_animation.mp4', writer=writer)然而,我想要的是gif,而不是视频,我希望能够在Google Colab中做到这一点。运行以下命令可以在避免压缩工件的同时转换动画。(其中一些参数来自this answer。
!ffmpeg -i test_animation.mp4 -vf "split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 test_animation.gif我已经更新了Google Colab notebook。
https://stackoverflow.com/questions/66522639
复制相似问题