0

enter image description hereHi I want to divide an image (200x200) into 100 blocks of equal sizes and then I want to find average of each block. I have looked around a lot on how to divide the image into 10x10 blocks(10 rows 10 columns) but not able to grasp the concept on how to do so. Can anyone help.

1 Answers1

3

I'll assume you have numpy, since you have it as a tag. If you don't have the Pillow module, run

pip install Pillow

and grab that. The following code will split the image into a 400 blocks of 10x10.

import numpy as np
from PIL import Image

image = Image.open("your_file.jpg", "r")
arr = np.asarray(image)
arr = np.split(arr, 20)
arr = np.array([np.split(x, 20, 1) for x in arr])

Then, to grab the i-j'th block, index into it via:

arr[i][j]
Daniel Nguyen
  • 419
  • 2
  • 7