簡體   English   中英

設置 matplotlib 視頻動畫大小 (1920x1080)

[英]Set matplotlib video animation size (1920x1080)

我正在嘗試創建一個1920x1080的 matplotlib 動畫。 下面生成的動畫是(1152x648),比例合適但是太小了。 我知道我可以在事后使用 ffmpeg 做到這一點,但如果可能的話,我想避免重新編碼。

y = my_data
fig = plt.figure(figsize=(16,9))

def ani(i):
    # Animation Code

animator = FuncAnimation(fig, ani, frames=y.shape[0])

# Can't figure out what extra_args to set here
ffmpeg_writer = FFMpegWriter(fps=y.shape[0]/sound.duration_seconds, extra_args=['-scale', '1920x1080'])
animator.save('mymovie.mp4', writer=ffmpeg_writer)

print(ffmpeg_writer.frame_size) # prints (1152, 648)

ipd.Video('mymovie.mp4')

正如您在上面看到的,我嘗試了一些extra_args ,但是我無法進行任何工作。

您可以按照以下帖子中的說明設置DPI參數。

您系統的標稱 DPI 為 72,因此分辨率為72*16 x 72*9 = 1152 x 648
我們可以將 DPI 設置為 1920/16 以獲得 1920x1080 的分辨率:

fig = plt.figure(figsize=(16, 9), dpi=1920/16)

這是一個可執行代碼示例(用於測試):

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.animation as animation

# 72 is the nominal DPI that is the reason figsize=(16, 9) resolution is (1152, 648).
# Set the DPI to 120 for getting resolution is (1920, 1080)
fig = plt.figure(figsize=(16, 9), dpi=(1920/16))
ax = plt.gca()  # Get current axes

l1, = ax.plot(np.arange(1, 10), np.arange(1, 10), **{'marker':'o'})

def init():
    l1.set_data([],[])
    
def ani(i, l1):
    l1.set_data(i/5, np.sin(0.5*i)*4+5)
    l1.set_markersize(10)

animator  = animation.FuncAnimation(fig, ani, fargs=(l1,), init_func=init, frames=50, interval=1, blit=False)  # 50 frames, interval=1

ffmpeg_writer = animation.FFMpegWriter(fps=10)
animator.save('mymovie.mp4', writer=ffmpeg_writer)

plt.show()  # Show for testing

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM