显示图像(Python版)

import numpy as np
import matplotlib.pyplot as plt

'''
显示单幅图像 
'''
def showSingleImg(img, path, name=None, isgray=False):
    plt.figure('')  # 图像窗口名称 figsize=(,) 设置窗口大小
    # 单幅图像
    plt.title('')
    if isgray:
        plt.imshow(img, cmap='gray')  # 灰度图
    else:
        plt.imshow(img)  # 彩图
    plt.axis('off')

    # 去除白边保存图片
    # 获得当前的figure对象 Get Current Figure
    if name is not None:
        savePath = path + name
        fig = plt.gcf()
        width, height = img.shape[0], img.shape[1]
        fig.set_size_inches(width / 96.0, height / 96.0)  # 图片的dpi:96.0
        plt.subplots_adjust(top=1, bottom=0, left=0, right=1, hspace=0, wspace=0)
        plt.margins(0, 0)
        plt.savefig(savePath  + '.tif', dpi=96.0, pad_inches=0.0)

    plt.show()


def get_row_col(num_pic):
    squr = num_pic ** 0.5
    row = round(squr)
    col = row + 1 if squr - row > 0 else row
    return row, col


'''
显示多张特征图 多通道
img:shape = [batch, height, width, channels] type:numpy.ndarray 
'''
def showMultipleFeatureMap(img, name=None):
    # 从数组的形状中删除单维条目,即把shape中为0的维度去掉
    img = np.squeeze(img, axis=0)
    num_pic = img.shape[2]
    row, col = get_row_col(num_pic)

    plt.figure("特征提取:")  # 图像窗口名称 figsize=(,) 设置窗口大小
    plt.suptitle('')
    for i in range(0, num_pic):
        plt.subplot(row, col, i)
        plt.title('')
        plt.imshow(img[:, :, i], cmap='gray')
        plt.axis('off')

    if name is not None:
        savePath = './' + name + '.png'
        plt.savefig(savePath)

    plt.show()


'''
显示多幅图像  channels = 1 或 3
img:shape = [batch, height, width, channels] type:numpy.ndarray 
'''
def showMultipleImg(img, name=None):
    num_pic = img.shape[0]
    row, col = get_row_col(num_pic)

    plt.figure("多幅图像:")  # 图像窗口名称 figsize=(,) 设置窗口大小
    plt.suptitle('')
    for i in range(0, num_pic):
        plt.subplot(row, col, i)
        plt.title('')
        if img.shape[3] == 1:
            plt.imshow(img[i, :, :, :], cmap='gray')  # 灰度图
        else:
            plt.imshow(img)  # 彩图
        plt.axis('off')

    if name is not None:
        savePath = './' + name + '.png'
        plt.savefig(savePath)

    plt.show()

if __name__ == '__main__':
    path = './data/'
    name = '0000_cluster'
    img = np.load(path + name + '.npy')
    print(img)
    showSingleImg(img, path, name, isgray=False)

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Python显示图像,可以使用多种库,如matplotlib、OpenCV和PIL。以下是使用matplotlib库来显示图像的示例代码: ```python import matplotlib.pyplot as plt import matplotlib.image as mpimg # 读取图片 img = mpimg.imread('path/to/image.jpg') # 显示图片 plt.imshow(img) plt.axis('off') # 不显示坐标轴 plt.show() ``` 在这个例子中,我们首先导入需要的库,然后使用`mpimg.imread()`函数读取图像。接下来,使用`plt.imshow()`函数将图像显示出来。最后,使用`plt.axis('off')`函数来隐藏坐标轴,使图像更加美观。最后使用`plt.show()`函数展示图像。这样就能在Python显示图像了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [使用Python查看并显示图像](https://blog.csdn.net/weixin_44850744/article/details/124759278)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [python实现读取并显示图片的两种方法](https://blog.csdn.net/sinat_38682860/article/details/114367815)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值