This is erosion, followed by dilatation, common operations in computer vision:
>>> from scipy.ndimage.morphology import binary_dilation, binary_erosion >>> print(list1) [0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1] >>> print(binary_dilation(binary_erosion(list1)).astype(int)) [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]
The composition of two operations is called discovery:
>>> from scipy.ndimage.morphology import binary_opening >>> print(binary_opening(list1).astype(int)) [0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1]