LoginSignup
8
7

More than 5 years have passed since last update.

かっこいい3DをPythonで書くブログを見つけた

このブログがかっこいいので、日本語版を買ってに作って共有!

結論から言うとこのブログだけでは、Python3ではうまくいかない。
そもそも images2gifがpython2である。

そこで、「images2gif python3」とググるとすぐに以下のGithubにたどり着く

これをローカルでとりあえず、python images2gif.pyしてうまく行けばよし。以下のGifができる。

test.gif

そして、上のブログのやつのと統合!

もしもすでにpython3でimages2gifをインストールしてしまった人は、pip uninstall images2gifかなんかで消す。

そして、直接上のGithubから持ってきたimages2gif.pyをimportする。

必要なものをImport(blogからそのまま利用)

import pandas as pd, numpy as np, random
import matplotlib.pyplot as plt, matplotlib.cm as cm
from mpl_toolkits.mplot3d import Axes3D
import IPython.display as IPdisplay
import glob
from PIL import Image as PIL_Image
from images2gif import writeGif

3dの画像をAxes3Dで書く(自分で勝手に書いた)

gif_file_name = '3d_test'

x = np.arange(-3, 3, 0.25)
y = np.arange(-3, 3, 0.25)

x = np.arange(-3, 3, 0.25)
y = np.arange(-3, 3, 0.25)
X, Y = np.meshgrid(x, y)
Z = np.sin(X)+ np.cos(Y)

fig = plt.figure()
ax = Axes3D(fig)
ax.plot_wireframe(X, Y, Z)

アングルの違う100枚の画像を保存(blogをそのまま利用)

ax.elev = 89.9
ax.azim = 270.1
ax.dist = 11.0

for n in range(100):
    if n >= 20 and n <= 22:
        ax.set_xlabel('')
        ax.set_ylabel('') #don't show axis labels while we move around, it looks weird
        ax.elev -= 0.5 #start by panning down slowly
    if n >= 23 and n <= 36:
        ax.elev -= 1.0 #pan down faster
    if n >= 37 and n <= 60:
        ax.elev -= 1.5
        ax.azim += 1.1 #pan down faster and start to rotate
    if n >= 61 and n <= 64:
        ax.elev -= 1.0
        ax.azim += 1.1 #pan down slower and rotate same speed
    if n >= 65 and n <= 73:
        ax.elev -= 0.5
        ax.azim += 1.1 #pan down slowly and rotate same speed
    if n >= 74 and n <= 76:
        ax.elev -= 0.2
        ax.azim += 0.5 #end by panning/rotating slowly to stopping position
    if n == 77:
        ax.set_xlabel('X')
        ax.set_ylabel('Y')
        ax.set_zlabel('Z')
    fig.suptitle('Practice')
    fig.savefig('images/' + 'img' + str(n).zfill(3) + '.png')

最後に100個の画像からgif作成

images = [PIL_Image.open(image) for image in glob.glob('images/*.png')]
file_path_name = 'images/3d_test.gif'
writeGif(file_path_name, images, duration=0.1)

これで完成したのが、以下になります!(画像が圧縮されてGifが動かないw…)

3d_test.gif

しかたがないので、5コマバージョン作成↓

3d_test_sm.gif

8
7
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
8
7