def clip(image, lower=None, upper=None):
    """
    Clip values to the range specified by `lower` and `upper`.
    """

    # assert that the input array remains unchanged
    image = image.copy()

    # clip
    if lower is not None:
        image[image < lower] = lower
    if upper is not None:
        image[image > upper] = upper

    return image