1

I have a 2D array like below, where 1,2,3...6 are rows(6) and Alphabets are columns(4).

1 - A,B,C,D
2 - E,F,G,H
3 - I,J,K,L
4 - M,N,O,P
5 - Q,R,S,T
6 - U,V,W,X 

I need to reshape this array to 2X3 array as mentioned below in a way that if I access "1" it should return alphabets A,B,C,D

1, 2, 3
4, 5, 6

after reshaping I will save the array as multi-band tiff.

I know it's a very simple task and I can do it by creating an empty array and filling it using for loops, but I want to do it with reshape function or any simple method.

Kindly help me guys.

gis.rajan
  • 517
  • 3
  • 20

2 Answers2

0

Your array format is a bit misleading. However, here's a minimal example I could prepare:

# example array
arr = pd.np.random.randint(1, 30, 30).reshape(-1, 5)
array([[25, 13, 24, 10, 14],
       [13, 11,  2, 24, 20],
       [16, 28,  5, 12, 24],
       [ 2, 21, 24, 29, 21],
       [21,  5, 18, 23, 23],
       [22,  9, 10, 29,  9]])

# reshape the array by taking first value from each row
np.apply_along_axis(lambda x: x[0], 1, arr).reshape(-1, 3)

array([[25, 13, 16],
       [ 2, 21, 22]])
YOLO
  • 20,181
  • 5
  • 20
  • 40
0

Got answer from this link

how to save an array representing an image with 40 band to a .tif file

Actually I was saving tiff file using skimage's imsave command and skimage can handle only 4 channel data. Following command solve my issue.

tifffile.imsave("y.tif", x, planarconfig='contig') ie. band dimension last for contig.
gis.rajan
  • 517
  • 3
  • 20