1

I have 6 different images. I want to store them together in a single numpy array. Is that possible? If yes, how can I do that?

from PIL import Image
from matplotlib import image
import matplotlib.pyplot as plt
from os import listdir
from numpy import asarray
import numpy as np

for i in range(1,6):
    image=Image.open(str(i)+'.jpg')
    image=image.resize((100,100))
    temp=asarray(image)
    print(np.append(X_train,temp,axis=0))

This raises the following Exception:

ValueError: all the input arrays must have same number of dimensions

0

1 Answer 1

2

you can create a list of arrays and the convert back to numpy array

list_of_pics = list()
for i in range(1,6):
    image=Image.open(str(i)+'.jpg')
    image=image.resize((100,100))
    list_of_pics.append(np.asarray(image))
new_array = np.array(list_of_pics)

the final dimentions of new_array should be (6,100,100)

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

2 Comments

I am getting shape as (6,100,100,3) but I want shape (6,100,100)
I think you're getting exactly what you want, because the 3 last dimensions are the RGB values that define the color value. If you're not interested in the color you can use the mean value for the brigthness, in this way you can convert the image in black and white. Check this en.wikipedia.org/wiki/Brightness

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.