How to animate a time-ordered sequence of matplotlib plots

后端 未结 3 1307
悲&欢浪女
悲&欢浪女 2020-12-14 03:20

I want to plot a sequence of .png images in matplotlib. The goal is to plot them rapidly to simulate the effect of a movie, but I have additional reasons for wanting to avoi

3条回答
  •  失恋的感觉
    2020-12-14 04:17

    I have implemented a handy script that just suits your need. Try it out here

    Below is a example that show images together with its bounding box:

    import os
    import glob
    from scipy.misc import imread
    from matplotlib.pyplot import Rectangle
    
    video_dir = 'YOUR-VIDEO-DIRECTORY'
    
    img_files = glob.glob(os.path.join(video_dir, '*.jpg'))
    box_files = glob.glob(os.path.join(video_dir, '*.txt'))
    
    def redraw_fn(f, axes):
        img = imread(img_files[f])
        box = bbread(box_files[f])  # Define your own bounding box reading utility
        x, y, w, h = box
        if not redraw_fn.initialized:
            im = axes.imshow(img, animated=True)
            bb = Rectangle((x, y), w, h,
                           fill=False,  # remove background
                           edgecolor="red")
            axes.add_patch(bb)
            redraw_fn.im = im
            redraw_fn.bb = bb
            redraw_fn.initialized = True
        else:
            redraw_fn.im.set_array(img)
            redraw_fn.bb.set_xy((x, y))
            redraw_fn.bb.set_width(w)
            redraw_fn.bb.set_height(h)
    redraw_fn.initialized = False
    
    videofig(len(img_files), redraw_fn, play_fps=30)
    

提交回复
热议问题