亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在導入的圖形中添加顏色條 (Python)

在導入的圖形中添加顏色條 (Python)

qq_笑_17 2022-12-20 12:21:45
我有一個灰度圖。我想知道如何使用 Python 在導入的圖形中添加顏色條。我知道如何繪制二維數組并添加顏色條,但我不知道如何為導入的圖形執行類似的任務。我還想保留原始圖像的大小。我用導入圖from PIL import Image im= Image.open("gray.png")提前致謝。
查看完整描述

1 回答

?
holdtom

TA貢獻1805條經驗 獲得超10個贊

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")

我已經從這里改編了部分答案



查看完整回答
反對 回復 2022-12-20
  • 1 回答
  • 0 關注
  • 131 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號