Jaskaran Singh Puri
Jaskaran Singh Puri

Reputation: 739

How to convert a python list of 3D arrays to a single 3D array of specific shape?

I have a list itemlist that has 25 3D Arrays with shape (128x128x3)

I want it to convert/merge all these values into a single common array, basically create a image out of it. I'm expecting the new shape to be (640, 640, 3) meaning 5 rows and 5 columns of (128, 128)

I tried the following, but it is giving weird results, mostly repeating some arrays:

out = np.concatenate(itemlist).ravel()
out.shape ##(1228800,)

img = np.reshape(out, (640,640,3))
img.shape ## (640, 640, 3) 

The final shape I get is correct but visually it looks like set of repeated images, is something wrong with logic?

Upvotes: 0

Views: 496

Answers (1)

hpaulj
hpaulj

Reputation: 231665

With 25 (128,128,3) arrays

out = np.concatenate(itemlist)

should produce a (25*128, 128,3) array

out = out.ravel()

should produce 25128128*3

out.shape ##(1228800,)

(640,640,3) matches in total number of elements, but it will not produce meaningful images.

Working backwards:

(5*128, 5*128,3)  => (5,128,5,128,3) => (5,5,128,128,3) => (25,128,128,3)

That requires a couple of reshapes, and one tranpose.

Upvotes: 3

Related Questions