util.py 文件源码

python
阅读 20 收藏 0 点赞 0 评论 0

项目:alphabetic 作者: hitokun-s 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号