24

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 = 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

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

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

3 Answers 3

36

Getting a list of BMP files

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

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

To combine all the images into one array:

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

Pickling a numpy array

To save a numpy array to file using pickle:

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.

Alternatively, numpy arrays can be pickled using methods supplied by numpy (hat tip: tegan). To dump array x in file file.npy, use:

x.dump('file.npy')

To load array x back in from file:

x = np.load('file.npy')

For more information, see the numpy docs for dump and load.

9
  • Thanks a lot . It worked . Another question if you don't mind can i load this numpy array into pickle ?
    – Md Shopon
    Commented Aug 28, 2016 at 20:22
  • 1
    x is the numpy array (as shown in the answer, for example). filehandle is the handle for the pickle file, such as open('filename.p', 'wb'). protocol=2 just tells pickle to use its current format rather than some ancient out-of-date format.
    – John1024
    Commented Aug 28, 2016 at 20:43
  • 1
    @omatai Yes, it is very useful. It is called list comprehension. As I don't have any favorite tutorials on the subject, I'll offer this link which will lead you to lots of information.
    – John1024
    Commented Jan 30, 2018 at 0:08
  • 1
    You don't have to import pickle to save numpy arrays; numpy can do this by itself (I think it uses pickle anyway, but saves you an import / worrying about protocols): myarray.dump("good_filename.npy") This lets you do the entire thing in one line, if you like: np.array([np.array(Image.open(fname)) for fname in filelist]).dump("good_filename.npy")
    – tegan
    Commented Mar 19, 2018 at 19:12
  • 2
    @tegan Very nice! Thanks. Answer updated with info on numpy.ndarray.dump and numpy.load.
    – John1024
    Commented Mar 20, 2018 at 0:22
6

Use OpenCV's imread() function together with os.listdir(), like

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'

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.

0

I just would like to share two sites where one can split a dataset into train, test and validation sets: split_folder and create numpy arrays out of images residing in respective folders code snippet from medium by muskulpesent

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.