Gayan Jeewantha
Gayan Jeewantha

Reputation: 31

Convert numpy array of a image into blocks

I have of a numpy array of a image.I want to convert this image into 8*8 block using python.How should I do this?

Upvotes: 1

Views: 973

Answers (2)

HYRY
HYRY

Reputation: 97331

reshape and then swapaxes:

import numpy as np

img = np.random.randint(0, 255, size=(128, 256, 3)).astype(np.uint8)

blocks = img.reshape(img.shape[0]//8, 8, img.shape[1]//8, 8, 3).swapaxes(1, 2)
print(blocks.shape)

to check the result:

np.allclose(blocks[0, 0], img[:8, :8, :])
np.allclose(blocks[3, 2], img[3*8:3*8+8, 2*8:2*8+8, :])

Upvotes: 2

SivaTP
SivaTP

Reputation: 153

Please provide your array structure.

you can use img_arrary.reshape(8,8), to work total elements must be 64

Upvotes: 1

Related Questions