はじめに
1枚の画像を分割して表示するアニメーションをmatplotlibのFuncAnimationによって作成する。
コード
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from IPython.display import HTML
from mpl_toolkits.axes_grid1 import ImageGrid
image = plt.imread('P_20200716_190011 (1).jpg')
fig, ax = plt.subplots(figsize=(8, 8))
ax.imshow(image)
ax.set_axis_off()
plt.show()
def image_div(image,fig, div):
image_list = []
image_size = image.shape[0]
im_size = int(image_size/div)
for i in range(div):
for l in range(div):
im = image[im_size*i:im_size*(i+1)+1,im_size*l:im_size*(l+1)+1]
image_list.append(im)
#fig = plt.figure(figsize=(8, 8))
grid = ImageGrid(fig, 111,
nrows_ncols=(div, div),
axes_pad=0.05,
share_all=True,
)
for i in range(len(image_list)):
grid[i].imshow(image_list[i])
grid[i].set_axis_off()
fig = plt.figure(figsize=(8, 8))
def update(i):
plt.clf()
image_div(image,fig,i+1)
return fig,
ani = animation.FuncAnimation(fig, update, 15,interval=333)
ani.save('img_vunkatsu.mp4', writer="ffmpeg",dpi=100)
HTML(ani.to_html5_video())
解説
モジュールのインポート
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.animation as animation
from IPython.display import HTML
from mpl_toolkits.axes_grid1 import ImageGrid
バージョン
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#version
print(np.__version__)
import matplotlib
print(matplotlib.__version__)
1.19.2
3.3.2
画像の読み込み
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image = plt.imread('P_20200716_190011 (1).jpg')
plt.imreadで画像を読みこむ。
画像を分割する関数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def image_div(image,fig, div):
image_list = []
image_size = image.shape[0]
im_size = int(image_size/div)
for i in range(div):
for l in range(div):
im = image[im_size*i:im_size*(i+1)+1,im_size*l:im_size*(l+1)+1]
image_list.append(im)
#fig = plt.figure(figsize=(8, 8))
grid = ImageGrid(fig, 111,
nrows_ncols=(div, div),
axes_pad=0.05,
share_all=True,
)
for i in range(len(image_list)):
grid[i].imshow(image_list[i])
grid[i].set_axis_off()
画像を分割するコードは下記記事とほぼ同じものを用いた。

[matplotlib] 43. 画像を2n×2nに分割して表示
画像を2x2,4x4や8x8に分割して表示する方法について説明する。
アニメーションの表示
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig = plt.figure(figsize=(8, 8))
def update(i):
plt.clf()
image_div(image,fig,i+1)
return fig,
ani = animation.FuncAnimation(fig, update, 15,interval=333)
ani.save('img_vunkatsu.mp4', writer="ffmpeg",dpi=100)
HTML(ani.to_html5_video())
figを作成して、それをアニメーション関数内でクリアして再び表示することでアニメーションとする。FuncAnimationでアニメーションを表示する。frame数を15,intervalを333msとして5秒ほどのアニメーションとする。 HTML(ani.to_html5_video())
により、jupyter notebook またはjupyter lab上にアニメーションを表示できる。
ani.save(‘ファイル名’, writer=”ffmpeg”,dpi=100)でアニメーションをMP4形式で保存することができる。
コードをダウンロード(.pyファイル) コードをダウンロード(.ipynbファイル)参考

[matplotlib] 43. 画像を2n×2nに分割して表示
画像を2x2,4x4や8x8に分割して表示する方法について説明する。
コメント