【问题标题】:Viewing .npy images查看 .npy 图像
【发布时间】:2016-02-02 11:52:03
【问题描述】:

如何查看以.npy 扩展名存储的图像并以该格式保存我自己的文件?

【问题讨论】:

    标签: python arrays image numpy


    【解决方案1】:

    .npy 是 numpy 数组的文件扩展名 - 您可以使用 numpy.load 读取它们:

    import numpy as np
    
    img_array = np.load('filename.npy')
    

    查看它们的最简单方法之一是使用 matplotlib 的 imshow 函数:

    from matplotlib import pyplot as plt
    
    plt.imshow(img_array, cmap='gray')
    plt.show()
    

    你也可以使用PIL or pillow:

    from PIL import Image
    
    im = Image.fromarray(img_array)
    # this might fail if `img_array` contains a data type that is not supported by PIL,
    # in which case you could try casting it to a different dtype e.g.:
    # im = Image.fromarray(img_array.astype(np.uint8))
    
    im.show()
    

    这些函数不是 Python 标准库的一部分,因此您可能需要安装 matplotlib 和/或 PIL/pillow(如果尚未安装)。我还假设文件是​​ 2D [rows, cols](黑白)或 3D [rows, cols, rgb(a)](颜色)像素值数组。

    【讨论】:

      猜你喜欢
      • 2021-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-24
      • 1970-01-01
      相关资源
      最近更新 更多