code: def extend_slice(s, shape, pad=1):
      r
      pad = int(pad)
      a = []
      for i, dim in zip(s, shape):
          start = 0
          stop = dim
          if i.start - pad >= 0:
              start = i.start - pad
          if i.stop + pad < dim:
              stop = i.stop + pad
          a.append(slice(start, stop, None))
      return tuple(a)
docs: r"""
      Adjust slice indices to include additional voxles around the slice.
  
      This function does bounds checking to ensure the indices don't extend
      outside the image.
  
      Parameters
      ----------
      s : list of slice objects
           A list (or tuple) of N slice objects, where N is the number of
           dimensions in the image.
  
      shape : array_like
          The shape of the image into which the slice objects apply.  This is
          used to check the bounds to prevent indexing beyond the image.
  
      pad : int
          The number of voxels to expand in each direction.
  
      Returns
      -------
      slices : list of slice objects
          A list slice of objects with the start and stop attributes respectively
          incremented and decremented by 1, without extending beyond the image
          boundaries.
  
      Examples
      --------
      >>> from scipy.ndimage import label, find_objects
      >>> from porespy.tools import extend_slice
      >>> im = sp.array([[1, 0, 0], [1, 0, 0], [0, 0, 1]])
      >>> labels = label(im)[0]
      >>> s = find_objects(labels)
  
      Using the slices returned by ``find_objects``, set the first label to 3
  
      >>> labels[s[0]] = 3
      >>> print(labels)
      [[3 0 0]
       [3 0 0]
       [0 0 2]]
  
      Next extend the slice, and use it to set the values to 4
  
      >>> s_ext = extend_slice(s[0], shape=im.shape, pad=1)
      >>> labels[s_ext] = 4
      >>> print(labels)
      [[4 4 0]
       [4 4 0]
       [4 4 2]]
  
      As can be seen by the location of the 4s, the slice was extended by 1, and
      also handled the extension beyond the boundary correctly.