Reputation: 1176
I am trying to handle a RAW image using numpy.
Printing the raw image numpy array returns this:
[[372 387 247 ... 560 359 364]
[380 392 243 ... 599 342 356]
[236 238 358 ... 355 600 564]
...
[547 553 344 ... 74 69 69]
[349 328 560 ... 68 74 73]
[341 334 537 ... 70 71 73]]
with shape 4384, 5632
.
I want to obtain a list of lists (2D list), such that each entry will correspond to a 8 * 8
square of the 2d numpy array.
This would indicate a list of dimensions 4384/8 , 5632/8
.
I can only think of using while loops and appending each square into the list at the moment, but I was thinking there must be a better way.
Is there a more pythonic way of doing it, maybe using list comprehension and slicing?
Upvotes: 0
Views: 206
Reputation: 551
Please find below a short explanation of the answer
# 1. Reshape Image as (height/8, 8, width/8,8)
print(rawimg.reshape(rawimg.shape[0]//8, 8, -1, 8).shape)
# 2. Swap 1 and 2nd index
print(rawimg.reshape(rawimg.shape[0]//8, 8, -1, 8).swapaxes(1,2).shape)
# 3. Reshape again (-1,8,8) with -1 being combined both (548*704) arrays of shape 8x8
print(rawimg.reshape(rawimg.shape[0]//8, 8, -1, 8).swapaxes(1,2).reshape(-1,8,8).shape)
(548, 8, 704, 8)
(548, 704, 8, 8)
(385792, 8, 8)
Upvotes: 1