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?

2
  • 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? Commented Jun 14, 2021 at 6:11
  • Question has nothing to do with artificial-intelligence - kindly do not spam irrelevant tags (removed). Commented Jun 21, 2021 at 0:13

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.