for j in range(img.shape[1]):
    if j > K:
        col_sums[:, (j)] = col_sums[:, (j - 1)] - row_sums[:, (j - K - 1)
            ] + row_sums[:, (j)]
Efficient processing of pixel + neighborhood in numpy image