在导入的图形中添加一个颜色条 (Python)

问题描述 投票:0回答:1

我有一个灰度的图。我想知道如何使用Python在导入的图形中添加一个色条。我知道如何绘制一个二维数组并添加一个颜色条,但我不知道如何为一个导入的图形做类似的工作。我还想保留原始图像的大小。我用以下方法导入图形

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

先谢谢你。

`

python python-imaging-library colorbar
1个回答
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")

我已经改编了部分答案,来自 此处

© www.soinside.com 2019 - 2024. All rights reserved.