'How to pad an image using np.pad in python?

I'm currently processing images that consist of a stack, 18 images per stack. I then deconvolve these images to produce cleaner sharper images. However when doing this I get border artifacts. I have spent some time writing code so as to determine how wide a pad I would need to pad these images, however I am unsure of how to use np.pad so that I may produce padded images. This is my code so far:

xextra = pad_width_x / 2
yextra = pad_width_y / 2
print (xextra)
print (yextra)

Where xextra and yextra are the pad widths I will be using. I understand that I will need to use this line of code to pad the array:

no_borders = np.pad(sparsebeadmix_sheet_cubic_deconvolution, pad_width_x, mode='constant', constant_values=0)

However how will I be able to process my stack of images (18 images) through this and save them as outputs?

I hope this makes sense!



Solution 1:[1]

If your stack is a nxny18 array:

import numpy as np

image_stack = np.ones((2, 2, 18))

extra_left, extra_right = 1, 2
extra_top, extra_bottom = 3, 1

np.pad(image_stack, ((extra_top, extra_bottom), (extra_left, extra_right), (0, 0)),
       mode='constant', constant_values=3) 

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 keramat