def animate(image_arrays, title="demo", size_inch=3, format="mp4"):
print "Let's create awesome animation!"
dpi = 100
fig = plt.figure()
ax = fig.add_subplot(111)
ax.set_aspect('equal')
ax.get_xaxis().set_visible(False)
ax.get_yaxis().set_visible(False)
height, width = image_arrays[0].shape
image_count = len(image_arrays)
im = ax.imshow(rand(width, height), cmap='gray', interpolation='nearest')
im.set_clim([0, 1])
fig.set_size_inches([size_inch, size_inch]) # size of created video
tight_layout()
def update_img(n):
im.set_data(image_arrays[n])
return im
# legend(loc=0)
ani = animation.FuncAnimation(fig, update_img, frames=image_count, interval=30)
filename = ""
if format == "mp4":
writer = animation.writers['ffmpeg'](fps=30)
filename = '%s.mp4' % title
ani.save(filename, writer=writer, dpi=dpi)
elif format == "gif":
filename = '%s.gif' % title
ani.save(filename, writer='imagemagick')
else:
print "unsupported format type!:%s" % format
return
print "%s is created!" % filename
return ani
# test to creating animation
评论列表
文章目录