示例#1
0
def add_colorbar_to_image(figure_fname,
                          data_max,
                          data_min,
                          colors_map,
                          background_color='black',
                          cb_ticks=[],
                          cb_ticks_font_size=10,
                          cb_title='',
                          set_cb_max_min_using_ticks=True,
                          cb_ticks_perc=2,
                          **kargs):
    fol = utils.get_fname_folder(figure_fname)
    if ',' in background_color:
        background_color = [float(x) for x in background_color.split(',')]
    if len(cb_ticks) == 0:
        cb_ticks = [data_min, data_max]
    cb_fname = op.join(fol, '{}_colorbar.jpg'.format(colors_map))
    plot_color_bar(data_max,
                   data_min,
                   colors_map,
                   do_save=True,
                   cb_ticks=cb_ticks,
                   fol=fol,
                   background_color=background_color,
                   cb_ticks_font_size=cb_ticks_font_size,
                   cb_title=cb_title,
                   set_cb_max_min_using_ticks=set_cb_max_min_using_ticks,
                   cb_ticks_perc=cb_ticks_perc)
    cb_img = Image.open(cb_fname)
    # crop_image(figure_fname, figure_fname, dx=150, dy=0, dw=150, dh=0)
    combine_brain_with_color_bar(figure_fname,
                                 cb_img,
                                 overwrite=True,
                                 cb_ticks=cb_ticks)
示例#2
0
def combine_brain_with_color_bar_old(data_max, data_min, figure_fname, colors_map, overwrite=False, dpi=100,
                                 x_left_crop=0, x_right_crop=0, y_top_crop=0, y_buttom_crop=0,
                                 w_fac=2, h_fac=3/2, facecolor='black', ticks=None,
                                 dy=0.03, ddh=0.92, ddw=0.8, dx=-0.1, **kargs):
    image_fol = utils.get_fname_folder(figure_fname)
    if not overwrite:
        image_fname = op.join(image_fol, '{}_cb.{}'.format(figure_fname[:-4], figure_fname[-3:]))
    else:
        image_fname = figure_fname
    # if op.isfile(image_fname) and not overwrite:
    #     return
    image = Image.open(figure_fname)
    img_width, img_height = image.size
    w, h = (img_width/dpi) * w_fac, (img_height/dpi) * h_fac
    fig = plt.figure(figsize=(w, h), dpi=dpi, facecolor=facecolor)
    fig.canvas.draw()
    gs = gridspec.GridSpec(18, 18)
    gs.update(wspace=0, hspace=0)  # set the spacing between axes.
    brain_ax = plt.subplot(gs[:, :-2])
    plt.tight_layout()
    plt.axis('off')
    im = brain_ax.imshow(image, animated=True)
    ax_cb = plt.subplot(gs[:, -2:-1])
    ax_cb.tick_params(axis='y', colors='white' if facecolor=='black' else 'black')
    resize_and_move_ax(ax_cb, dy=dy, dx=dx, ddh=ddh, ddw=ddw)
    plot_color_bar(data_max, data_min, colors_map, ax_cb, cb_ticks=ticks)
    plt.savefig(image_fname, facecolor=fig.get_facecolor(), transparent=True)
    plt.close()
    image = Image.open(image_fname)
    w, h = image.size
    image.crop((x_left_crop, y_top_crop, w-x_right_crop, h-y_buttom_crop)).save(image_fname)
示例#3
0
def combine_two_images_and_add_colorbar(lh_figure_fname, rh_figure_fname, new_image_fname, data_max, data_min,
        colors_map, background_color='black', cb_ticks=[], cb_ticks_font_size=10, add_cb=True, crop_figures=True,
        remove_original_figures=False, cb_title='', **kargs):
    fol = utils.get_fname_folder(lh_figure_fname)
    if ',' in background_color:
        background_color = [float(x) for x in background_color.split(',')]
    if add_cb:
        if crop_figures:
            # crop_image(lh_figure_fname, lh_figure_fname, dx=150, dy=0, dw=50, dh=70)
            # crop_image(rh_figure_fname, rh_figure_fname, dx=150 + 50, dy=0, dw=0, dh=70)
            crop_image(lh_figure_fname, lh_figure_fname, dx=100, dy=0, dw=50, dh=0)
            crop_image(rh_figure_fname, rh_figure_fname, dx=100, dy=0, dw=0, dh=0)

        combine_two_images(lh_figure_fname, rh_figure_fname, new_image_fname, facecolor=background_color,
                           dpi=200, w_fac=1, h_fac=1)

        cb_fname = op.join(fol, '{}_colorbar.jpg'.format(colors_map))
        _, img_height = Image.open(lh_figure_fname).size
        plot_color_bar(data_max, data_min, colors_map, do_save=True, cb_ticks=cb_ticks, fol=fol,
                       facecolor=background_color, cb_ticks_font_size=cb_ticks_font_size, title=cb_title) # h=img_height*0.7, w=img_height*0.2
        cb_img = Image.open(cb_fname)

        combine_brain_with_color_bar(new_image_fname, cb_img, overwrite=True, cb_ticks=cb_ticks)
    else:
        if crop_figures:
            crop_image(lh_figure_fname, lh_figure_fname, dx=150, dy=0, dw=150, dh=0)
            crop_image(rh_figure_fname, rh_figure_fname, dx=150, dy=0, dw=150, dh=0)
        combine_two_images(lh_figure_fname, rh_figure_fname, new_image_fname, facecolor=background_color)
    if remove_original_figures:
        if lh_figure_fname != new_image_fname:
            utils.remove_file(lh_figure_fname)
        if rh_figure_fname != new_image_fname:
            utils.remove_file(rh_figure_fname)
示例#4
0
def example6():
    figures_fol = '/home/npeled/mmvt/nmr01216/figures'
    colors_map = 'RdOrYl'
    data_max, data_min = 2, 6
    background = '#393939'

    files = glob.glob(op.join(figures_fol, '*.png'))
    images_hemi_inv_list = set([
        utils.namebase(fname)[3:] for fname in files
        if utils.namebase(fname)[:2] in ['rh', 'lh']
    ])
    files = [[
        fname for fname in files if utils.namebase(fname)[3:] == img_hemi_inv
    ] for img_hemi_inv in images_hemi_inv_list]
    for files_coup in files:
        hemi = 'rh' if utils.namebase(files_coup[0]).startswith('rh') else 'lh'
        coup_template = files_coup[0].replace(hemi, '{hemi}')
        coup = {}
        for hemi in utils.HEMIS:
            coup[hemi] = coup_template.format(hemi=hemi)
        new_image_fname = op.join(utils.get_fname_folder(files_coup[0]),
                                  utils.namebase_with_ext(files_coup[0])[3:])

        fu.crop_image(coup['lh'], coup['lh'], dx=150, dy=0, dw=150, dh=0)
        fu.crop_image(coup['rh'], coup['rh'], dx=150, dy=0, dw=0, dh=0)
        fu.combine_two_images(coup['lh'],
                              coup['rh'],
                              new_image_fname,
                              facecolor=background)
        fu.combine_brain_with_color_bar(data_max,
                                        data_min,
                                        new_image_fname,
                                        colors_map,
                                        dpi=200,
                                        overwrite=True,
                                        w_fac=1.2,
                                        h_fac=1.2,
                                        ddh=0.7,
                                        dy=0.13,
                                        ddw=0.4,
                                        dx=-0.08)
        for hemi in utils.HEMIS:
            utils.remove_file(coup[hemi])
示例#5
0
def combine_brain_with_color_bar(image_fname, cb_img=None, w_offset=10, overwrite=False, cb_max=None, cb_min=None,
                                 cb_cm=None, background='black', cb_ticks=[], cb_ticks_font_size=10):
    if cb_img is None:
        if len(cb_ticks) == 0:
            cb_ticks = [cb_min, cb_max]
        fol = utils.get_parent_fol(image_fname)
        cb_fname = op.join(fol, '{}_colorbar.jpg'.format(cb_cm))
        plot_color_bar(cb_max, cb_min, cb_cm, do_save=True, cb_ticks=cb_ticks, fol=fol, background_color=background,
                       cb_ticks_font_size=cb_ticks_font_size)
        cb_img = Image.open(cb_fname)

    background = Image.open(image_fname)
    bg_w, bg_h = background.size
    cb_w, cb_h = cb_img.size
    offset = (int((bg_w - cb_w)) - w_offset, int((bg_h - cb_h) / 2))
    background.paste(cb_img, offset)
    if not overwrite:
        image_fol = utils.get_fname_folder(image_fname)
        image_fname = op.join(image_fol, '{}_cb.{}'.format(image_fname[:-4], image_fname[-3:]))
    background.save(image_fname)
    return image_fname
示例#6
0
def post_blender_call(args):
    if not args.add_cb and not args.join_hemis:
        return

    from src.utils import figures_utils as fu
    from src.utils import utils
    from PIL import Image

    if args.call_mmvt_calls:
        su.waits_for_file(args.log_fname)
    with open(args.images_log_fname, 'r') as text_file:
        images_names = text_file.readlines()
    images_names = [l.strip() for l in images_names]
    data_max, data_min = list(map(float, args.cb_vals))
    ticks = list(map(float,
                     args.cb_ticks)) if args.cb_ticks is not None else None
    background = args.background_color  # '#393939'
    if args.join_hemis:
        images_hemi_inv_list = set([
            utils.namebase(fname)[3:] for fname in images_names
            if utils.namebase(fname)[:2] in ['rh', 'lh']
        ])
        files = [[
            fname for fname in images_names
            if utils.namebase(fname)[3:] == img_hemi_inv
        ] for img_hemi_inv in images_hemi_inv_list]
        fol = utils.get_fname_folder(files[0][0])
        cb_fname = op.join(fol, '{}_colorbar.jpg'.format(args.cb_cm))
        # if not op.isfile(cb_fname):
        fu.plot_color_bar(data_max,
                          data_min,
                          args.cb_cm,
                          do_save=True,
                          ticks=ticks,
                          fol=fol,
                          background_color=background,
                          cb_ticks_font_size=args.cb_ticks_font_size)
        cb_img = Image.open(cb_fname)
        for files_coup in files:
            hemi = 'rh' if utils.namebase(
                files_coup[0]).startswith('rh') else 'lh'
            coup_template = files_coup[0].replace(hemi, '{hemi}')
            coup = {
                hemi: coup_template.format(hemi=hemi)
                for hemi in utils.HEMIS
            }
            new_image_fname = op.join(
                fol,
                utils.namebase_with_ext(files_coup[0])[3:])
            if args.add_cb:
                if args.crop_figures:
                    fu.crop_image(coup['lh'],
                                  coup['lh'],
                                  dx=150,
                                  dy=0,
                                  dw=50,
                                  dh=70)
                    fu.crop_image(coup['rh'],
                                  coup['rh'],
                                  dx=150 + 50,
                                  dy=0,
                                  dw=0,
                                  dh=70)
                fu.combine_two_images(coup['lh'],
                                      coup['rh'],
                                      new_image_fname,
                                      facecolor=background,
                                      dpi=200,
                                      w_fac=1,
                                      h_fac=1)
                fu.combine_brain_with_color_bar(new_image_fname,
                                                cb_img,
                                                overwrite=True)
            else:
                if args.crop_figures:
                    fu.crop_image(coup['lh'],
                                  coup['lh'],
                                  dx=150,
                                  dy=0,
                                  dw=150,
                                  dh=0)
                    fu.crop_image(coup['rh'],
                                  coup['rh'],
                                  dx=150,
                                  dy=0,
                                  dw=150,
                                  dh=0)
                fu.combine_two_images(coup['lh'],
                                      coup['rh'],
                                      new_image_fname,
                                      facecolor=background)
            if args.remove_temp_figures:
                for hemi in utils.HEMIS:
                    utils.remove_file(coup[hemi])
    elif args.add_cb and not args.join_hemis:
        fol = utils.get_fname_folder(images_names[0])
        cb_fname = op.join(fol, '{}_colorbar.jpg'.format(args.cb_cm))
        if not op.isfile(cb_fname):
            fu.plot_color_bar(data_max,
                              data_min,
                              args.cb_cm,
                              do_save=True,
                              ticks=ticks,
                              fol=fol,
                              background_color=background)
        cb_img = Image.open(cb_fname)
        for fig_name in images_names:
            fu.combine_brain_with_color_bar(fig_name, cb_img, overwrite=True)