0

i'm trying to creat numpy array from my list that contains are 1768 images. this my code:

w = []
directory = os.listdir(PATH)
directory = sorted(directory, key=len)
for item in directory:
    img = Image.open(os.path.join(PATH, item))
    w.append(img)
    count+=1
print('w_shape: ', np.array(w, dtype='float32').shape)

and when run it, i face with this error:

TypeError: float() argument must be a string or a number, not 'JpegImageFile'

anybody can help me to solve?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
mohamad
  • 23
  • 1
  • 1
  • 4
  • 2
    What do you want the resulting array to look like? What should its `shape` be? What problem do you hope to solve by creating the array? – Karl Knechtel Jun 14 '21 at 06:11
  • Question has nothing to do with `artificial-intelligence` - kindly do not spam irrelevant tags (removed). – desertnaut Jun 21 '21 at 00:13

1 Answers1

0

You are getting this error because each element of you list is a 'JpegImageFile' which is not compatible with float(). To avoid this, add the images directly as arrays to your list.
You can use np.asarray() to read the image as a numpy array. Check out the code snippet below:

w = []
directory = os.listdir(PATH)
directory = sorted(directory, key=len)
for item in directory:
    img = Image.open(os.path.join(PATH, item))
    w.append(np.asarray(img)) # convert it to an array of floats
    count+=1 
print('w_shape: ', np.array(w, dtype='float32').shape)

Note: You can also use np.array(img) instead. Check out what the difference is here

Shubham
  • 1,310
  • 4
  • 13