LoginSignup
45
49

More than 5 years have passed since last update.

MacでPythonからアニメーションGIFを生成する環境設定

Last updated at Posted at 2015-07-11

アニメーションGIFをPythonで作るために必要なライブラリ、ImageMagickとPythonMagickのインストールが一苦労だったので、やり方をここにメモします。

anim_test.gif
こんなのとか、
quadratic_decent_anim-compressor.gif
こんなのとか、
normdist_decent_0.0_anim-compressor.gif
こんなのが生成できます。

0.環境

  • OSX 10.10.3
  • Macbook (Retina, 12-inch, Early 2015)
  • Annaconda 3.10.0

1.Image Magickのインストール

sudo dscl . -append /Groups/wheel GroupMembership <ユーザー名>
sudo chmod -R g+w /usr/local/include/ 
sudo chmod -R g+w /usr/local/share/man/man5
sudo chmod -R g+w /usr/local/lib/
brew link libpng
brew install imagemagick

2.Python Magickのインストール

brew install boost
brew install boost-python
sudo chmod -R g+w /usr/local/share/info
brew link libtool

Python Magickのダウンロード
http://www.imagemagick.org/download/python/
 ※現時点では「PythonMagick-0.9.12.zip」が最新バージョン

cd /path/to/PythonMagick-0.9.12
./configure
make

エラーが出る。。。

error
CXXLD    _PythonMagick.la
clang: warning: argument unused during compilation: '-pthread'
ld: warning: directory not found for option '-L/usr/local/Cellar/freetype/2.6/lib'

調べてみると、自分の環境では

/usr/local/Cellar/freetype/2.6_1/lib/

が正しいパスのようだ。

修正すべきファイルを探す。

find ./ -type f -print | xargs grep 'freetype' -n

/path/to/PythonMagick-0.9.12の直下だったので、

vi _PythonMagick.la 

で、先ほどのパスのところを修正。
気を取り直して、

make
sudo make install

3.お試し

%matplotlib inline 
import matplotlib.pyplot as plt
import numpy as np
from matplotlib import animation as ani
plt.style.use('ggplot')

num_frame = 80.
x_range= 7

def animate(nframe):
    global num_frame
    plt.clf()
    a = ((5*nframe/num_frame) -.5) * np.pi
    x = np.linspace(-x_range, x_range, 200)
    y = np.sin(x+a)

    plt.xlim(-x_range, x_range)
    plt.ylim(-1.1,1.1)
    plt.plot(x, y, c="b")

fig = plt.figure(figsize=(12,5))
anim = ani.FuncAnimation(fig, animate, frames=int(num_frame))
anim.save('anim_test.gif', writer='imagemagick', fps=5, dpi=64)

出来上がり!

anim_test.gif

45
49
2

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
45
49