Home > Software design >  is there a way to flatten images in a list?
is there a way to flatten images in a list?

Time:09-15

I have 3 images, and i store them in a list C.

from sklearn.datasets import load_sample_images
datasets = load_sample_images()
image1 = [datasets.images[1][:,:,0],datasets.images[1][:,:,0],datasets.images[0][:,:,0]]
# datasets.images[1][:,:,0] is a Numpy array

For some reason, i want to flatten each of them. and stored the result in a NumPy array. My solution is

result = np.array([item.flatten() for item in image1])

Is there any better solution?

CodePudding user response:

First you should know the size of an image. for example 32 * 32 if it is RGB then the shape would be (3 * 32 * 32 * 3) (first 3 is number of images and second 3 is for RGB color layers). Now need to apply first flatten() and then apply reshape() to the array:

  data = np.array([np.array(cv2.imread(imagePath[i])) for i in range(len(imagePath))] )

  output = x_data.flatten().reshape(3, 3072)
  • Related