polygonmovie.py 文件源码

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

项目:CAAPR 作者: Stargrazer82301 项目源码 文件源码
def createpolygonmovie(coordfile, moviefile, background=None, xrange=(0,1), yrange=(0,1), shape=(500,500), rate=24):
    # read the coordinates
    polygons = np.loadtxt(os.path.expanduser(coordfile))

    # open the movie file
    movie = MovieFile(moviefile, shape=shape, rate=rate)

    # setup the figure and its background
    figure = plt.figure(1, dpi=100, figsize=(shape[0]/100.,shape[1]/100.))
    x0,x1 = xrange
    y0,y1 = yrange
    px,py = shape
    if background != None:
        # transpose and scale i,j indices to x,y values
        backim = np.fromfunction(lambda i,j: background((j+0.5)/px*(x1-x0)+x0,(i+0.5)/py*(y1-y0)+y0), shape[::-1])
        plt.imshow(backim, origin='lower', aspect='auto', interpolation='bicubic', extent=(x0,x1,y0,y1))
    plt.grid(True)
    plt.xlim(xrange[0], xrange[1])
    plt.ylim(yrange[0], yrange[1])

    # for each line in the file, draw the polygon and add a movie frame
    for p in polygons:
        plt.plot(np.concatenate((p[0::2],p[0:1])), np.concatenate((p[1::2],p[1:2])), 'w-')
        plt.draw()                                  # flush the drawing
        movie.add(figure.canvas.buffer_rgba(0,0))   # pass the pixel buffer on as movie frame
        figure.axes[0].lines.pop()                  # remove the plotted line

    # close things
    plt.close()
    movie.close()

# -----------------------------------------------------------------
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号