Python 如何在 numpy 数组中加载多个图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39195113/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to load multiple images in a numpy array ?
提问by Md Shopon
How to load pixels of multiple images in a directory in a numpy array . I have loaded a single image in a numpy array . But can not figure out how to load multiple images from a directory . Here what i have done so far
如何在 numpy 数组中的目录中加载多个图像的像素。我在 numpy array 中加载了单个图像。但无法弄清楚如何从一个目录加载多个图像。这是我到目前为止所做的
image = Image.open('bn4.bmp')
nparray=np.array(image)
This loads a 32*32 matrices . I want to load 100 of the images in a numpy array . I want to make 100*32*32 size numpy array . How can i do that ? I know that the structure would look something like this
这将加载 32*32 矩阵。我想在一个 numpy 数组中加载 100 个图像。我想制作 100*32*32 大小的 numpy array 。我怎样才能做到这一点 ?我知道结构看起来像这样
for filename in listdir("BengaliBMPConvert"):
if filename.endswith(".bmp"):
-----------------
else:
continue
But can not find out how to load the images in numpy array
但无法找到如何加载 numpy 数组中的图像
回答by John1024
Getting a list of BMP files
获取 BMP 文件列表
To get a list of BMP files from the directory BengaliBMPConvert
, use:
要从目录中获取 BMP 文件列表BengaliBMPConvert
,请使用:
import glob
filelist = glob.glob('BengaliBMPConvert/*.bmp')
On the other hand, if you know the file names already, just put them in a sequence:
另一方面,如果您已经知道文件名,只需将它们按顺序排列:
filelist = 'file1.bmp', 'file2.bmp', 'file3.bmp'
Combining all the images into one numpy array
将所有图像组合成一个 numpy 数组
To combine all the images into one array:
将所有图像合并为一个数组:
x = np.array([np.array(Image.open(fname)) for fname in filelist])
Pickling a numpy array
酸洗一个 numpy 数组
To save a numpy array to file using pickle:
使用 pickle 将 numpy 数组保存到文件:
import pickle
pickle.dump( x, filehandle, protocol=2 )
where x
is the numpy array to be save, filehandle
is the handle for the pickle file, such as open('filename.p', 'wb')
, and protocol=2
tells pickle to use its current format rather than some ancient out-of-date format.
x
要保存的 numpy 数组在哪里 ,filehandle
是泡菜文件的句柄,例如open('filename.p', 'wb')
,并protocol=2
告诉泡菜使用其当前格式而不是一些古老的过时格式。
Alternatively, numpy arrays can be pickled using methods supplied by numpy (hat tip: tegan). To dump array x
in file file.npy
, use:
或者,可以使用 numpy 提供的方法腌制 numpy 数组(提示:tegan)。要x
在 file 中转储数组file.npy
,请使用:
x.dump('file.npy')
To load array x
back in from file:
x
从文件加载数组:
x = np.load('file.npy')
回答by 2Obe
Use OpenCV's imread()function together with os.listdir(), like
将 OpenCV 的imread()函数与os.listdir()一起使用,例如
import numpy as np
import cv2
import os
instances = []
# Load in the images
for filepath in os.listdir('images/'):
instances.append(cv2.imread('images/{0}'.format(filepath),0))
print(type(instances[0]))
class 'numpy.ndarray'
类'numpy.ndarray'
This returns you a list (==instances
) in which all the greyscale values of the images are stored. For colour images simply set .format(filepath),1
.
这将返回一个列表 (== instances
),其中存储了图像的所有灰度值。对于彩色图像,只需设置.format(filepath),1
.
回答by bit_scientist
I just would like to share two sites where one can split a dataset into train, test and validation sets: split_folderand create numpy arrays out of images residing in respective folders code snippet from medium by muskulpesent
我只想分享两个站点,在其中可以将数据集拆分为训练集、测试集和验证集:split_folder并从位于相应文件夹中的图像中创建 numpy 数组来自媒体的代码片段由muskulpesent