def make_movie(root, movie_name='', fr=20, overwrite=False): if movie_name == '': movie_name = utils.namebase(root) tmp_output_fname = op.join(root, 'new_images', '{}.mp4'.format(movie_name)) output_fname = op.join(utils.get_parent_fol(root), '{}.mp4'.format(movie_name)) if not op.isfile(output_fname) or overwrite: mu.combine_images(root, movie_name, frame_rate=fr, copy_files=True) if op.isfile(tmp_output_fname): shutil.move(tmp_output_fname, output_fname) if op.isdir(op.join(root, 'new_images')): shutil.rmtree(op.join(root, 'new_images'))
def ani_frame(time_range, xticks, images, dpi, fps, video_fname, cb_data_type, data_to_show_in_graph, fol, fol2, cb_title='', cb_min_max_eq=True, cb_norm_percs=None, color_map='jet', cb2_data_type='', cb2_title='', cb2_min_max_eq=True, color_map2='jet', bitrate=5000, images2=(), ylim=(), ylabels=(), xticklabels=(), xlabel='Time (ms)', show_first_pic=False, show_animation=False, overwrite=True): def two_brains_two_graphs(): if cb2_data_type == '': brain_ax = plt.subplot(gs[:-g2, :g3]) else: brain_ax = plt.subplot(gs[:-g2, 1:g3 + 1]) brain_ax.set_aspect('equal') brain_ax.get_xaxis().set_visible(False) brain_ax.get_yaxis().set_visible(False) image = mpimg.imread(images[0]) im = brain_ax.imshow( image, animated=True) #, cmap='gray',interpolation='nearest') if cb2_data_type == '': brain_ax2 = plt.subplot(gs[:-g2, g3:-1]) else: brain_ax2 = plt.subplot(gs[:-g2, g3 + 1:-1]) brain_ax2.set_aspect('equal') brain_ax2.get_xaxis().set_visible(False) brain_ax2.get_yaxis().set_visible(False) image2 = mpimg.imread(images2[0]) im2 = brain_ax2.imshow( image2, animated=True) #, cmap='gray',interpolation='nearest') graph1_ax = plt.subplot(gs[-g2:, :]) graph2_ax = graph1_ax.twinx() if cb_data_type != '': ax_cb = plt.subplot(gs[:-g2, -1]) else: ax_cb = None if cb2_data_type != '': ax_cb2 = plt.subplot(gs[:-g2, -1]) ax_cb = plt.subplot(gs[:-g2, 0]) else: ax_cb2 = None plt.tight_layout() resize_and_move_ax(brain_ax, dx=0.04) resize_and_move_ax(brain_ax2, dx=-0.00) if cb2_data_type != '': resize_and_move_ax(ax_cb2, ddw=0.5, ddh=0.9, dx=-0.04, dy=0.03) resize_and_move_ax(ax_cb, ddw=0.5, ddh=0.9, dx=0.03, dy=0.03) resize_and_move_ax(brain_ax, dx=-0.03) resize_and_move_ax(brain_ax2, dx=-0.04) elif cb_data_type != '': resize_and_move_ax(ax_cb, ddw=0.5, ddh=0.8, dx=-0.01, dy=0.06) for graph_ax in [graph1_ax, graph2_ax]: resize_and_move_ax(graph_ax, dx=0.04, dy=0.03, ddw=0.89) # if cb2_data_type != '': # resize_and_move_ax(graph_ax, ddh=1.2) return ax_cb, im, im2, graph1_ax, graph2_ax, ax_cb2 def one_brain_one_graph(gs, g2, two_graphs=False): brain_ax = plt.subplot(gs[:-g2, :-1]) brain_ax.set_aspect('equal') brain_ax.get_xaxis().set_visible(False) brain_ax.get_yaxis().set_visible(False) image = mpimg.imread(images[0]) im = brain_ax.imshow( image, animated=True) #, cmap='gray',interpolation='nearest') graph1_ax = plt.subplot(gs[-g2:, :]) graph2_ax = graph1_ax.twinx() if two_graphs else None ax_cb = plt.subplot(gs[:-g2, -1]) plt.tight_layout() # resize_and_move_ax(brain_ax, dx=0.03) resize_and_move_ax(ax_cb, ddw=1, dx=-0.06) resize_and_move_ax(graph1_ax, dx=0.05, dy=0.03, ddw=0.89) if not graph2_ax is None: resize_and_move_ax(graph2_ax, dx=0.05, dy=0.03, ddw=0.89) return ax_cb, im, graph1_ax, graph2_ax first_image = Image.open(images[0]) img_width, img_height = first_image.size print('video: width {} height {} dpi {}'.format(img_width, img_height, dpi)) img_width_fac = 2 if fol2 != '' else 1.1 w, h = img_width / dpi * img_width_fac, img_height / dpi * 3 / 2 fig = plt.figure(figsize=(w, h), dpi=dpi, facecolor='white') fig.canvas.draw() g = 15 g2 = int(g / 3) g3 = int((g - 1) / 2) gs = gridspec.GridSpec(g, g) #, height_ratios=[3, 1]) if fol2 != '': if cb2_data_type != '': gs = gridspec.GridSpec(g, g + 1) # , height_ratios=[3, 1]) ax_cb, im, im2, graph1_ax, graph2_ax, ax_cb2 = two_brains_two_graphs() else: two_graphes = len(data_to_show_in_graph) == 2 ax_cb, im, graph1_ax, graph2_ax = one_brain_one_graph( gs, g2, two_graphes) im2, ax_cb2 = None, None # gs.update(left=0.05, right=0.48, wspace=0.05) # graph_data, graph_colors, t_line, ymin, ymax = plot_graph( # graph1_ax, data_to_show_in_graph, fol, fol2, graph2_ax, ylabels) graph_data, graph_colors, t_line, ymin, ymax = plot_graph( graph1_ax, data_to_show_in_graph, time_range, xticks, fol, fol2, graph2_ax, xlabel, ylabels, xticklabels, ylim, images) if not ax_cb2 is None and fol2 != '': graph_data2, _ = utils.load(op.join(fol2, 'data.pkl')) plot_color_bar(ax_cb, graph_data, cb_title, cb_data_type, cb_min_max_eq, cb_norm_percs, color_map, 'left') plot_color_bar(ax_cb2, graph_data2, cb2_title, cb2_data_type, cb2_min_max_eq, cb_norm_percs, color_map2) else: plot_color_bar(ax_cb, graph_data, cb_title, cb_data_type, cb_min_max_eq, cb_norm_percs, color_map) now = time.time() if show_first_pic: plt.show() def init_func(): return update_img(0) def update_img(image_index): # print(image_fname) utils.time_to_go(now, image_index, len(images)) image = mpimg.imread(images[image_index]) im.set_data(image) if im2: image2 = mpimg.imread(images2[image_index]) im2.set_data(image2) current_t = get_t(images, image_index, time_range) if not current_t is None: t_line.set_data([current_t, current_t], [ymin, ymax]) # print('Reading image {}, current t {}'.format(images[image_index], current_t)) return [im] else: return None if show_animation: ani = animation.FuncAnimation(fig, update_img, len(images), init_func=init_func, interval=1000, blit=True, repeat=False) plt.show() # Set up formatting for the movie files # Writer = animation.writers['ffmpeg'] #FFMpegWriter # # Writer = animation.AVConvWriter # writer = Writer(fps=fps, bitrate=1800) #, extra_args=['-vcodec', 'libx264']) # ani.save(op.join(fol, video_fname), writer=writer) # writer = animation.writers['ffmpeg'](fps=fps, bitrate=bitrate) # ani.save(op.join(fol, video_fname), writer=writer, dpi=dpi) else: images_fol = utils.get_parent_fol(images[0]) new_images_fol = op.join(images_fol, 'movie_images') utils.make_dir(new_images_fol) images_nb = utils.namebase(images_fol) for image_index in range(len(images)): new_image_fname = op.join(new_images_fol, 'mv_{}.png'.format(image_index)) if not op.isfile(new_image_fname) or overwrite: img = update_img(image_index) if not img is None: plt.savefig(new_image_fname, facecolor=fig.get_facecolor(), transparent=True) movie_fname = op.join(utils.get_parent_fol(images_fol), images_nb) if op.isfile('{}.mp4'.format(movie_fname)) and overwrite: utils.remove_file('{}.mp4'.format(movie_fname)) mu.combine_images(new_images_fol, movie_fname, frame_rate=fps, movie_name_full_path=True)
if __name__ == '__main__': subject = 'mg106' frames_fol = 'MG106_HGP_sess2_4' fname = 'MG106_HGP_sess2_2' times_per_frame = 0.1 cut_in_break = False # last_frame_len = 5 * 60 times_field = 'time2' times = load_times(subject, '{}.mat'.format(fname), times_field) last_frame_len = np.diff(times)[0] frames = load_frames(subject, frames_fol_name=frames_fol) break_ind = get_breakind(times) if cut_in_break else len(times) print('max time: {} minutes'.format( times[break_ind if cut_in_break else len(times) - 1] / 60)) new_frames = duplicate_frames(frames, times, times_per_frame, last_frame_len, max_frames_num=break_ind) dup_fol = create_dup_frames_links(subject, new_frames, '{}_dup'.format(frames_fol)) movie_fname = movu.combine_images(dup_fol, frames_fol, frame_rate=int(1 / times_per_frame)) new_movie_fname = op.join(MMVT_DIR, subject, 'figures', utils.namebase_with_ext(movie_fname)) utils.remove_file(new_movie_fname) shutil.move(movie_fname, new_movie_fname)
def create_movie(fol, name): mu.combine_images(fol, name, 10, images_prefix='rotation_new_', copy_files=True)