1
votes

I am trying to load a bunch of image pixel data into a numpy array but facing issues with figuring out how to populate the dtype field.

My images are 128 x 128 x 3 in size with type uint8 and are loaded as follows:

import glob
from scipy import misc
images = np.fromiter((misc.imread(path) for path in glob.glob('images/*.png')), <dtype_field>)

misc.imread loads each image as a numpy array, but the issue I am facing is turning this list of images itself into a numpy array.

Tried the following as well but doesn't work:

images = [misc.imread(path) for path in glob.glob('extra_signs/*.png')]
images = np.asarray(images)

Where i get the following error:

ValueError: could not broadcast input array from shape (128,128,3) into shape (128,128)

Setting dtype to np.uint8 throws the following error:

ValueError: setting an array element with a sequence.

Note: I've looked around stackoverflow, I found questions on how to read an image into a numpy array, but none on reading all the images into a single array.

1
It looks like one of your images is not 128x128x3. Are you sure there isn't a monochrome image with shape 128x128 among your files? - Warren Weckesser
@WarrenWeckesser Is monochrome image just a fancy word for grayscale image? - kmario23
@WarrenWeckesser You were almost right. Turns out I had an image with a depth of 4 channels. Not sure what went wrong there but I replaced it with another one that had a normal depth of 3 and it worked. Thanks Warren! - Moataz Elmasry
@WarrenWeckesser Please post this as an answer so I can accept it and close this question. Thanks! - Moataz Elmasry

1 Answers

4
votes

Check the size all the images that you are reading. Apparently at least one of them is not 128x128x3.