Generalized technique :
import numpy as np
arr = np.array([[[ 0, 1, 2],
[ 6, 7, 8]],
[[ 3, 4, 5],
[ 9, 10, 11]],
[[12, 13, 14],
[18, 19, 20]],
[[15, 16, 17],
[21, 22, 23]]])
print(arr)
block_shape = (2, 3)
blockVertical = arr.shape[0] // block_shape[0]
blockHorizontal = arr.shape[0] // blockVertical
reshapedArray1 = arr.reshape(blockVertical,blockHorizontal,*block_shape)
'''
[[[[ 0 1 2]
[ 6 7 8]]
[[ 3 4 5]
[ 9 10 11]]]
[[[12 13 14]
[18 19 20]]
[[15 16 17]
[21 22 23]]]]
'''
reshapedArray2 = reshapedArray1.swapaxes(1,2)
'''
[[[[ 0 1 2]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]]
[[[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]]]]
'''
res = reshapedArray2.reshape(blockVertical * block_shape[0], blockHorizontal * block_shape[1])
'''
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
'''
Method 2 :
import numpy as np
arr = np.array([[[ 0, 1, 2],
[ 6, 7, 8]],
[[ 3, 4, 5],
[ 9, 10, 11]],
[[12, 13, 14],
[18, 19, 20]],
[[15, 16, 17],
[21, 22, 23]]])
arrTransposed = arr.reshape(2,2,2,3)
print(arrTransposed)
'''
[[[[ 0 1 2]
[ 6 7 8]]
[[ 3 4 5]
[ 9 10 11]]]
[[[12 13 14]
[18 19 20]]
[[15 16 17]
[21 22 23]]]]
'''
res1 = arrTransposed.swapaxes(1,2).reshape(4,6)
print(res1)
'''
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
'''
Method 3(Einsum) :
import numpy as np
arr = np.array([[[ 0, 1, 2],
[ 6, 7, 8]],
[[ 3, 4, 5],
[ 9, 10, 11]],
[[12, 13, 14],
[18, 19, 20]],
[[15, 16, 17],
[21, 22, 23]]])
# Use einsum
result_einsum = np.einsum('abcd -> acbd', arr.reshape(2, 2, 2, 3)).reshape(4, 6)
print(result_einsum)
'''
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
'''
Method 4 :
import numpy as np
from skimage.util import view_as_blocks
# Define the input array
arr = np.array([[[ 0, 1, 2],
[ 6, 7, 8]],
[[ 3, 4, 5],
[ 9, 10, 11]],
[[12, 13, 14],
[18, 19, 20]],
[[15, 16, 17],
[21, 22, 23]]])
# Specify the block shape for the 3D array
block_shape = (2,1,3)
# Use view_as_blocks to split the array into blocks
blocks = view_as_blocks(arr, block_shape)
print(blocks)
'''
[[[[ [[ 0 1 2]]
[[ 3 4 5]]]]
[[[[ 6 7 8]]
[[ 9 10 11] ]]]]
[[[[[12 13 14]]
[[15 16 17]]]]
[[[[18 19 20]]
[[21 22 23]]]]]]
'''
# Reshape and rearrange the blocks into the desired 2D array
blocks_reshaped = blocks.reshape(2, 2, 2, 3)#.reshape(4, 6)
print(blocks_reshaped)
'''
[[[[ 0 1 2]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]]
[[[12 13 14]
[15 16 17]]
[[18 19 20]
[21 22 23]]]]
'''
res = blocks_reshaped.reshape(4,6)
print(res)
'''
[[ 0 1 2 3 4 5]
[ 6 7 8 9 10 11]
[12 13 14 15 16 17]
[18 19 20 21 22 23]]
'''