1

I have some numpy arrays which its elements are pixels of 28*28 images like this:

enter image description here

25 of these arrays are in one array in shape of (25,28,28) or (5,5,28,28). Is there any efficient way to stack them to have one image: 5*5 of 28*28 images.

enter image description here

I tried np.reshape to (140,140) array and plt.imgshow. But the output was a messed image.

1
  • Huh? How can you get 625 images of 28x28 in an array of 25x28x28? Commented Dec 30, 2019 at 11:59

1 Answer 1

3

"I tried np.reshape to (140,140)..." That will work if you first transpose the input appropriately.

Suppose the input x has shape (5, 5, 28, 28). To get the array y with shape (140, 140) that contains the images arranged the way you want, you can do:

xshp = x.shp
y = x.transpose((0, 2, 1, 3)).reshape((xshp[0]*xshp[2], xshp[1]*xshp[3]))

If x always has shape (5, 5, 28, 28), you can hardcode the constant 140:

y = x.transpose((0, 2, 1, 3)).reshape((140, 140))

For example, here I create x with shape (5, 5, 28, 28) where each 28x28 image is a constant. The constants are chosen randomly. The tranposed, reshaped array y is plotted, and you can see that all the constant blocks are arranged correctly.

In [148]: rng = np.random.default_rng()                                                                       

In [149]: x = np.repeat(rng.integers(0, 256, size=(5, 5)), 28*28, axis=-1).reshape((5, 5, 28, 28))            

In [150]: y = x.transpose((0, 2, 1, 3)).reshape((140, 140))                                                   

In [151]: imshow(y) 

plot

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.