LoginSignup
0
0

animation.FuncAnimationでmp4動画の作成がうまくいかなかった際の対処法

Posted at

問題

PCを新しくしたので、ImageMagickの最新版を入れて、matplotlib.animation.FuncAnimationでグラフを動かそうとしたら問題が起きましあ。
以下のように、毎回のフレームで画像サイズが変わってしまいます。この点を解決したいです。
gifで作成すると以下のように正常に出力されます。
output.gif

しかし、mp4で作成すると最初の1枚目は通常に出力されますが、2枚目からは拡大された画像が出力されます。
image.png
mp4の1枚目の画像
image.png
mp4の2枚目の画像

環境

windows 11
imagemagick7.1.1
matplotlib 3.7.2

ソースコード

上のサンプルコードは https://qiita.com/yubais/items/c95ba9ff1b23dd33fde2 からいただきました。

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

fig = plt.figure()

def plot(data):
    plt.cla()                      # 現在描写されているグラフを消去
    rand = np.random.randn(100)    # 100個の乱数を生成
    plt.plot(rand)            # グラフを生成
    plt.xlabel('x')

ani = animation.FuncAnimation(fig, plot, interval=100, frames=10)
ani.save("output.gif", writer="imagemagick", dpi=100)

解決法

matplotlibrcのANIMATIONのセクションのanimation.writer: ffmpegを有効にしたら解決しました。
つまり、imagemagickが悪さしていたようです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0