As a quick example:
import Image import numpy as np im = Image.open('temp.png') data = np.array(im) flattened = data.flatten() print data.shape print flattened.shape
This gives:
(612, 812, 4) (1987776,)
Alternatively, instead of calling data.flatten()
you can call data.reshape(-1)
. -1
used as a placeholder to "determine what this measurement should be."
Note that this will give the vector ( flattened
) r0, g0, b0, r1, g1, b1, ... rn, gn, bn
, while you need the vector r0, r1 ... rn, b0, b1, ... bn, g0, g1, ... gn
.
To get exactly what you want, just call
flattened = data.T.flatten()
instead.
source share