I have 1000 RGB images which I want to read from the current directory and store it in a numpy array in the shape of (1000,3,32,32) for using it in CNN.

For this reason, I have read a sample image, resized it to 32 * 32. Then appended it to an array 'a' which I have created using zeros for the shape (1000,3,32,32). But I am getting an error called " 'numpy.ndarray' an object has no attribute 'append' ". How can it be solved? If it needs any different approach I am open to that as well.

import matplotlib.pyplot as plt

import numpy as np

reshapedimage =cv2.resize(cv2.imread("0 (1).png", 1), (32, 32))


a = np.zeros((1000,3,32,32))

a.append(reshapedimage)

1 Answers

0
Mark Setchell On Best Solutions

I think you mean this:

import numpy as np

# Create dummy image-like thing
w, h = 32, 32
im=np.arange(h*w*3).reshape((3,h,w))                                                       

# Create empty list
stack=[]                                                                                   

# Append the image to the stack 5 times
stack.append(im)
stack.append(im)
stack.append(im)
stack.append(im)
stack.append(im)                                                                          

# Make Numpy array and check size                                                        
v = np.array(stack)                                                                        
print(v.shape)                                                                                    

Output

(5, 3, 32, 32)