您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – Matplotlib.animation:如何删除白边

5b51 2022/1/14 8:22:20 python 字数 3692 阅读 537 来源 www.jb51.cc/python

我尝试使用matplotlib电影作家生成一部电影.如果我这样做,我总是在视频周围留下白色边缘.有谁知道如何删除该保证金? 调整后的例子来自http://matplotlib.org/examples/animation/moviewriter.html # This example uses a MovieWriter directly to grab individual frames and

概述

调整后的例子来自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()
fig.subplots_adjust(left=0,bottom=0,right=1,top=1,hspace=None)

如果重新使用ImageAxes对象,也可以使代码更有效

mat = np.random.random((100,100))
im = ax.imshow(mat,interpolation='nearest')
with writer.saving(fig,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.animation:如何删除白边全部内容,希望文章能够帮你解决python – Matplotlib.animation:如何删除白边所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶