Skip to content
Advertisement

Divide the image into 4×4 blocks and save each coordinate in variable

I have an grayscale Image of 128×128 that I want to divide into 4×4 pixel non-overlapping blocks and I want to save coordinate of each pixel as variable like this-

JavaScript

I know I can do it manually by defining variables, but I can use any for loop for making it faster? After that, I’ll find mean of each block by-

JavaScript

Any help?Suggestions?

Advertisement

Answer

To get the value of the pixel you could use PIL and you could use a for loop and append a tuple of the x and y coordinate like this:

JavaScript

this would work for the whole image for those blocks you could just add an extra loop

JavaScript

edit: If you want to use a grayscale image you should do this:

JavaScript

the first int in the tuple is the grayscale 0 (black) to 255 (white) the second int is the alpha but my guess would be that you don’t use alpha in a grayscale image

Advertisement