Добавление цветовой полосы в импортированную фигуру (Python)

У меня фигура в оттенках серого. Я хотел бы знать, как добавить цветовую полосу к такой импортированной фигуре с помощью Python. Я знаю, как построить 2D-массив и добавить цветовую полосу, но не могу понять, как выполнить аналогичную задачу для импортированной фигуры. Также хотелось бы сохранить размер исходного изображения. Я импортирую фигуру с

from PIL import Image
im= Image.open("gray.png")

Заранее спасибо.

`


person Ninpou    schedule 02.06.2020    source источник
comment
вы хотите что-то подобное, imgur.com/a/QSYOfgk!   -  person p47hf1nd3r    schedule 02.06.2020
comment
@Snehil ДА! Именно так. Не могли бы вы дать мне больше информации? Вы использовали команду над этим рисунком? Как?   -  person Ninpou    schedule 03.06.2020


Ответы (1)


from mpl_toolkits.axes_grid1 import make_axes_locatable
import matplotlib.pyplot as plt

def display_image_in_actual_size(im_path):

    dpi = 80
    im_data = plt.imread(im_path)
    height, width, depth = im_data.shape
    # What size does the figure need to be in inches to fit the image?
    figsize = width / float(dpi), height / float(dpi)

    # Create a figure of the right size with one axes that takes up the full figure
    fig = plt.figure(figsize=figsize)
    ax = fig.add_axes([0, 0, 1, 1])


    #ax.axis('off') #  uncommenting this will result  a plot without axis !
    # configures size of colorbar 
    divider = make_axes_locatable(ax)
    im=plt.imshow(im_data)
    cax = divider.append_axes("right", size="5%", pad=0.05)
    plt.colorbar(im, cax=cax)

    ax.imshow(im_data, cmap='gray')
    ax.set(xlim=[-10, width - 0.5], ylim=[height - 0.5, -0.5], aspect=1)

    plt.savefig('last_image.png')    #saving new image
    plt.show()

display_image_in_actual_size("gray.png")

Я адаптировал некоторые части ответа из здесь

person p47hf1nd3r    schedule 02.06.2020
comment
Большое тебе спасибо. Это именно то, что я искал. Проголосовали и приняли как правильный ответ. - person Ninpou; 03.06.2020