BadAss BadAss - 1 year ago 290
Python Question

How to load multiple images in a numpy array ?

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

image ='bn4.bmp')

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

for filename in listdir("BengaliBMPConvert"):
if filename.endswith(".bmp"):

But can not find out how to load the images in numpy array

Answer Source

To get a list of BMP files from the directory BengaliBMPConvert, use"

import glob
filelist = glob.glob('BengaliBMPConvert/*.bmp')

To combine all the images into one array:

x = np.array([np.array( for fname in filelist])

On the other hand, if you know the file names already, just put them in a sequence:

filelist = 'file1.bmp', 'file2.bmp', 'file3.bmp'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download