def contours_are_sorted(contours, reverse=False):
    """Check whether a list of contours is sorted."""
    for i, cont in enumerate(contours[:-1]):
        if not reverse and cont.lvl > contours[i + 1].lvl:
            return False
        if reverse and cont.lvl < contours[i + 1].lvl:
            return False
    return True