查看.npy图像 [英] Viewing .npy images

查看:860
本文介绍了查看.npy图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用我为项目下载的一些代码并学习Python时,该代码从中提取的某些文件是另存为.npy数据文件的图像.

While working with some code that I downloaded for a project and to learn Python with, some of the files that the code pulls from are images that are saved as .npy data files.

我对Pythonnumpy相对较新,发布之前浏览的很多资源都涉及保存为.npy的数字数据.是否可以查看以该扩展名存储的图像以及以该格式保存自己的文件?

I'm relatively new to Python and numpy and a lot of the resources I've browsed through before posting have been regarding number data saved as .npy. Is there a way that I can view the images that are stored with this extension as well as save my own files in that format?

推荐答案

.npy是numpy数组的文件扩展名-您可以使用

.npy is the file extension for numpy arrays - you can read them using numpy.load:

import numpy as np

img_array = np.load('filename.npy')

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

One of the easiest ways to view them is using matplotlib's imshow function:

from matplotlib import pyplot as plt

plt.imshow(img_array, cmap='gray')
plt.show()

您还可以使用 PIL或枕头:

You could also use 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)](彩色)阵列.如果不是这种情况,那么您将不得不告诉我们更多有关数组格式的信息,例如img_array.shapeimg_array.dtype是什么.

These functions aren't part of the Python standard library, so you may need to install matplotlib and/or PIL/pillow if you haven't already. I'm also assuming that the files are either 2D [rows, cols] (black and white) or 3D [rows, cols, rgb(a)] (color) arrays of pixel values. If that's not the case then you will have to tell us more about the format of the arrays, for example what img_array.shape and img_array.dtype are.

这篇关于查看.npy图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆