Given an array of [x,y] as follows, I'd like to return a new array with the average of the 3x3 sub-arrays. E.g. the following array:
image = [[7, 4, 0, 1],
[5, 6, 2, 2],
[6, 10, 7, 8],
[1, 4, 2, 0]]
Should return
new_image = [[5, 4],
[4, 4]]
I've managed to get this far at which point I can get the new array as a list of the form:
new_image = [5,4,4,4]
This is my approach:
h = list(range(1,len(image)-1))
w = list(range(1,len(image[0])-1))
def square_sum(image,i,j):
sum = image[i-1][j-1] + image[i-1][j] + image[i][j-1] + image[i+1][j] + image[i][j+1] + image[i+1][j+1] + image[i][j] + image[i-1][j+1] + image[i+1][j-1]
return sum//9
new_image = []
for i in h:
for j in w:
new_image.append(square_sum(image,i,j))
return(new_image)
So my question is how can I get my output to be an array of arrays instead of just a list. (I've realised it's a lot easier to do this with Numpy but would like to see how I could go about solving it in line with the above).