apples-oranges apples-oranges - 6 months ago 108
Python Question

Numpy flatten RGB image array

I have 1,000 RGB images (64X64) which I want to convert to an (m, n) array.

I use this:

import numpy as np
from skdata.mnist.views import OfficialImageClassification
from matplotlib import pyplot as plt
from PIL import Image
import glob
import cv2

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


Which gives me:
(1000, 64, 64, 3)


Now if I do:

pixels = x_data.flatten()
print pixels.shape


I get:
(12288000,)


However, I require an array with these dimensions:
(1000, 12288)


How can I achieve that?

Ray Ray
Answer

Apply the numpy method reshape() after applying flatten() to the flattened array:

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

  pixels = x_data.flatten().reshape(1000, 12288)
  print pixels.shape