I know I'm a bit late to this, but in case you wanted to perform relative padding (aka edge padding), here's how you can implement it. Note that the very first instance of assignment results in zero-padding, so you can use this for both zero-padding and relative padding (this is where you copy the edge values of the original array into the padded array).
def replicate_padding(arr):
"""Perform replicate padding on a numpy array."""
new_pad_shape = tuple(np.array(arr.shape) + 2) # 2 indicates the width + height to change, a (512, 512) image --> (514, 514) padded image.
padded_array = np.zeros(new_pad_shape) #create an array of zeros with new dimensions
# perform replication
padded_array[1:-1,1:-1] = arr # result will be zero-pad
padded_array[0,1:-1] = arr[0] # perform edge pad for top row
padded_array[-1, 1:-1] = arr[-1] # edge pad for bottom row
padded_array.T[0, 1:-1] = arr.T[0] # edge pad for first column
padded_array.T[-1, 1:-1] = arr.T[-1] # edge pad for last column
#at this point, all values except for the 4 corners should have been replicated
padded_array[0][0] = arr[0][0] # top left corner
padded_array[-1][0] = arr[-1][0] # bottom left corner
padded_array[0][-1] = arr[0][-1] # top right corner
padded_array[-1][-1] = arr[-1][-1] # bottom right corner
return padded_array
The optimal solution for this is numpy's pad method.
After averaging for 5 runs, np.pad with relative padding is only 8%
better than the function defined above. This shows that this is fairly an optimal method for relative and zero-padding padding.
#My method, replicate_padding
start = time.time()
padded = replicate_padding(input_image)
end = time.time()
delta0 = end - start
#np.pad with edge padding
start = time.time()
padded = np.pad(input_image, 1, mode='edge')
end = time.time()
delta = end - start
print(delta0) # np Output: 0.0008790493011474609
print(delta) # My Output: 0.0008130073547363281
print(100*((delta0-delta)/delta)) # Percent difference: 8.12316715542522%