import numpy as np
from skimage import measure as ms


def CondenseLabel(Label):
    """
    Shifts labels in a label image to fill in gaps corresponding to missing
    values.

    Parameters
    ----------
    Label : array_like
        A label image generated by segmentation methods.

    Returns
    -------
    Condensed : array_like
        A label image where all values > 0 are shifted down to fill gaps.

    See Also
    --------
    ShuffleLabel
    """

    # get list of unique object labels
    Unique = np.unique(Label.flatten())

    # remove background objects (Label == 0)
    Unique = np.delete(Unique, (Unique == 0).nonzero())

    # initialize output
    Condensed = np.zeros(Label.shape)

    # get pixel list for each object
    Props = ms.regionprops(Label.astype(np.int))

    # fill in new values
    for i in range(len(Unique)):
        Coords = Props[i].coords
        Condensed[Coords[:, 0], Coords[:, 1]] = i

    return Condensed

import numpy as np
from skimage import measure as ms


def CondenseLabel(Label):
    """
    Shifts labels in a label image to fill in gaps corresponding to missing
    values.

    Parameters
    ----------
    Label : array_like
        A label image generated by segmentation methods.

    Returns
    -------
    Condensed : array_like
        A label image where all values > 0 are shifted down to fill gaps.

    See Also
    --------
    ShuffleLabel
    """

    # get list of unique object labels
    Unique = np.unique(Label.flatten())

    # remove background objects (Label == 0)
    Unique = np.delete(Unique, (Unique == 0).nonzero())

    # initialize output
    Condensed = np.zeros(Label.shape, dtype=np.uint32)

    # get pixel list for each object
    Props = ms.regionprops(Label.astype(np.int))

    # fill in new values
    for i in range(len(Unique)):
        Coords = Props[i].coords
        Condensed[Coords[:, 0], Coords[:, 1]] = i

    return Condensed

Update CondenseLabel.py