1. 程式人生 > >python matplotlib 播放圖片變化過程

python matplotlib 播放圖片變化過程

open ext mage lib 分享 結果 animation 變化 mpeg

   最近想將原圖片和處理後的圖片放在一起觀察圖片的變化過程。但是網上並麽有找到有用的示例代碼,所以粘出來和大家分享一下。

import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from PIL import Image

img_ori = np.array(Image.open(5.png))
img_tran = np.load(filename.npy)
fig = plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(img_ori)
plt.subplot(
1, 2, 2) ims = [] i = [i for i in range(200) if i % 5 == 0] def update(i): return plt.imshow(img_tran[i].reshape([28, 28])) ani = animation.FuncAnimation(fig, update) ani.save(contrast.mp4, fps=15, extra_args=[-vcodec, libx264], writer=ffmpeg_file) plt.show()

運行結果是

python matplotlib 播放圖片變化過程