small_holes = [hole for hole in holes if 500 < im[hole].size < 1000]
for hole in small_holes:
    a, b, c, d = max(hole[0].start - 1, 0), min(hole[0].stop + 1, im.shape[
        0] - 1), max(hole[1].start - 1, 0), min(hole[1].stop + 1, im.shape[
        1] - 1)
im[a:b, c:d] = scipy.ndimage.morphology.binary_fill_holes(im[a:b, c:d]).astype(
    int) * 255
Scipy Binary Closing - Edge Pixels lose value