gpt4 book ai didi

python - 使用 matplotlib.animation 从 matplotlib 制作视频后创建的空 .mp4 文件

转载 作者:太空宇宙 更新时间:2023-11-04 05:23:07 25 4
gpt4 key购买 nike

我找到了这个小代码,我可以使用它保存视频(在网格中随机改变颜色):

import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
from scipy import rand

Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='xy'), bitrate=3600)

fig = plt.figure()
frames = []
for add in np.arange(15):
base = rand(10, 10)
frames.append((plt.pcolormesh(base, ),))

im_ani = animation.ArtistAnimation(fig, frames, interval=500, repeat_delay=3000,
blit=True)
im_ani.save('Vid.mp4', writer=writer)

我试着把它插入到我的模拟中,我没有得到任何错误,但是视频是空的,整个画面是白色的。你能帮我吗?这是我得到它的最简单的情况:

class Dummy():

def __init__(self):
self.video=[]

def addFrame(self):

Frame=rand(10,10)
print (Frame)
self.video.append((plt.pcolormesh(Frame),))

def saveVideo(self):

Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='XY'), bitrate=3600)
fig = plt.figure()

im_ani = animation.ArtistAnimation(fig, self.video, interval=500, repeat_delay=3000,
blit=True)
im_ani.save('myVid.mp4', writer=writer, dpi=dpi)

你可以试试看:

from scipy import rand

foo=Dummy()

for i in range(20):
foo.addFrame()

foo.saveVideo()

最佳答案

您的 self.video.append((plt.pcolormesh(Frame),)) 行没问题。您只是更改了语句的顺序。

import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np


class Dummy():
def __init__(self, fname):
self.fname = fname
self.video = []
self.fig, self.ax = plt.subplots() # Create new figure here

def addFrame(self):
Frame = np.random.rand(10, 10)
self.video.append((self.ax.pcolormesh(Frame), ))

def saveVideo(self):
Writer = animation.writers['ffmpeg']
writer = Writer(fps=15, metadata=dict(artist='XY'), bitrate=3600)
im_ani = animation.ArtistAnimation(self.fig, self.video, interval=500,
repeat_delay=3000, blit=True)
im_ani.save(self.fname, writer=writer)


foo1 = Dummy('foo1.mp4')
for i in range(20):
foo1.addFrame()
foo1.saveVideo()

foo2 = Dummy('foo2.mp4')
for i in range(40):
foo2.addFrame()
foo2.saveVideo()

关于python - 使用 matplotlib.animation 从 matplotlib 制作视频后创建的空 .mp4 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39680733/

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