So, I have the following code for resizing an image using nearest neighbor interpolation. The solution seems straightforward to me using 2 for loops, but I can't think of any way to do this while taking advantage of numpy to avoid those pesky loops. Here is my code:
def scale(img, factor):
# Calculate new image shape and create new image with it.
height, width = img.shape[:2]
new_height, new_width = (int(height * factor), int(width * factor))[:2]
scaled_img = np.zeros([new_height, new_width])
# Iterate over all pixels and set their values based on the input image.
for x in range(new_height):
for y in range(new_width):
scaled_img[x, y] = img[int(x / factor), int(y / factor)]
return scaled_img
Any input on how to avoid the for loops?