1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Mathematica でアニメーションを連番画像として出力する

Last updated at Posted at 2019-12-01

例えばこのようなコードでアニメーションを作ったとします。

Manipulate[
 ParametricPlot[ReIm@Sqrt[Exp[I t]], {t, 0, tmax},
  Epilog -> {Red, PointSize[0.02], Point[ReIm@Sqrt[Exp[I tmax]]]},
  PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}},
  AxesLabel -> (Style[#, 16] & /@ {Re, Im}),
  TicksStyle -> Directive[12],
  PlotLabel -> Style[Sqrt[Exp[I \[Theta]]], 20]
  ],
 {tmax, 0.01, 2 Pi}
 ]

このアニメーションを連番画像として出力するにはManipulateの中身をgとおいて、このようなコードに改造します。

dir = "/Users/star-yoshi/Mathematica/gallery";
Table[
 Module[{tmax, g, file},
  tmax = (2 Pi)/100 k;
  g = ParametricPlot[ReIm@Sqrt[Exp[I t]], {t, 0, tmax},
    Epilog -> {Red, PointSize[0.02], Point[ReIm@Sqrt[Exp[I tmax]]]},
    PlotRange -> {{-1.5, 1.5}, {-1.5, 1.5}},
    AxesLabel -> (Style[#, 16] & /@ {Re, Im}),
    TicksStyle -> Directive[12],
    PlotLabel -> Style[Sqrt[Exp[I t]], 20]
    ];
  file = FileNameJoin[{dir, ToString[NumberForm[k - 1,3, NumberPadding -> {"0", ""}]] <> ".png"}];
  Export[file, g]
  ],
 {k, 101}
 ]

dirは出力先の先のディレクトリのパスです。ディレクトリをターミナルにドラッグすると、パスに変換されるのでそれを使うと便利です。
NumberFormの第二引数は出力画像のファイル名のゼロ埋めを何個にするか決めます。
これを実行すると連番画像の出力が始まります。Tableの代わりにParallelTableを使うと並列化されて速くなります。

連番画像になったら今度はこれをffmpegなどで動画にすることができます。Mathematicaで直接動画にすることもできますが、
この方法のほうがたぶん速いです。あと再生速度の調整や同じ動画を違うフォーマットで作り直すというとき、連番画像にしておけばffmpegの入力を
変えるだけなので効率的です。

1
2
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
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?