这段代码主要实现的是读取一系列图片,然后将其保存为1080*720大小,帧率为10fps的视频
import matplotlib.pyplot as plt
from matplotlib.animation import FFMpegWriter
def traverse_imgs(writer, images):
# 遍历所有图片,并且让writer抓取视频帧
for img in images:
plt.imshow(img)
writer.grab_frame()
plt.pause(0.01)
plt.clf()
if __name__ == '__main__':
# 创建video writer, 设置好相应参数,fps
metadata = dict(title='01', artist='Matplotlib',comment='depth prediiton')
writer = FFMpegWriter(fps=10, metadata=metadata)
# 读出自己的所有图片
images = ....
figure = plt.figure(figsize=(10.8, 7.2))
plt.ion() # 为了可以动态显示
plt.tight_layout() # 尽量减少窗口的留白
with writer.saving(figure, 'out.mp4', 100):
traverse_imgs(writer, images)