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