gpt4 book ai didi

python - Matplotlib.动画 : how to remove white margin

转载 作者:太空狗 更新时间:2023-10-29 22:16:18 25 4
gpt4 key购买 nike

我尝试使用 matplotlib 电影编写器生成电影。如果我这样做,我总是会在视频周围出现白边。有谁知道如何删除该边距?

来自 http://matplotlib.org/examples/animation/moviewriter.html 的调整示例

# This example uses a MovieWriter directly to grab individual frames and
# write them to a file. This avoids any event loop integration, but has
# the advantage of working with even the Agg backend. This is not recommended
# for use in an interactive setting.
# -*- noplot -*-

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

FFMpegWriter = manimation.writers['ffmpeg']
metadata = dict(title='Movie Test', artist='Matplotlib',
comment='Movie support!')
writer = FFMpegWriter(fps=15, metadata=metadata, extra_args=['-vcodec', 'libx264'])

fig = plt.figure()
ax = plt.subplot(111)
plt.axis('off')
fig.subplots_adjust(left=None, bottom=None, right=None, wspace=None, hspace=None)
ax.set_frame_on(False)
ax.set_xticks([])
ax.set_yticks([])
plt.axis('off')

with writer.saving(fig, "writer_test.mp4", 100):
for i in range(100):
mat = np.random.random((100,100))
ax.imshow(mat,interpolation='nearest')
writer.grab_frame()

最佳答案

None 作为参数传递给 subplots_adjust 并没有按照您的想法进行 (doc) .这意味着“使用默认值”。要执行您想要的操作,请改用以下命令:

fig.subplots_adjust(left=0, bottom=0, right=1, top=1, wspace=None, hspace=None)

如果您重新使用 ImageAxes 对象,您还可以使您的代码更加高效

mat = np.random.random((100,100))
im = ax.imshow(mat,interpolation='nearest')
with writer.saving(fig, "writer_test.mp4", 100):
for i in range(100):
mat = np.random.random((100,100))
im.set_data(mat)
writer.grab_frame()

默认情况下,imshow 将宽高比固定为相等,也就是说您的像素是正方形的。您要么需要重新调整图形的大小,使其与图像的纵横比相同:

fig.set_size_inches(w, h, forward=True)

或告诉 imshow 使用任意纵横比

im = ax.imshow(..., aspect='auto')

关于python - Matplotlib.动画 : how to remove white margin,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15882395/

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