def rerange(image, old_min, old_max, new_min, new_max, dtype):
    """
    Return image with values in new range.

    Note: The default range of images is [0, 255] and most image
    processing functions expect this range and will fail otherwise.
    However, as input to neural networks re-ranged images, e.g [-1, +1]
    are sometimes needed.

    >>> import numpy as np
    >>> image = np.array([[0, 255], [255, 0]])
    >>> rerange(image, 0, 255, -1, +1, 'float32')
    array([[-1.,  1.],
           [ 1., -1.]], dtype=float32)

    :param numpy.array image: Should be a numpy array of an image.
    :param int|float old_min: Current minimum value of image, e.g. 0
    :param int|float old_max: Current maximum value of image, e.g. 255
    :param int|float new_min: New minimum, e.g. -1.0
    :param int|float new_max: New maximum, e.g. +1.0
    :param numpy datatype dtype: Data type of output image,
           e.g. float32' or np.uint8
    :return: Image with values in new range.
    """
    image = image.astype('float32')
    old_range, new_range = old_max - old_min, new_max - new_min
    image = (image - old_min) / old_range * new_range + new_min
    return image.astype(dtype)
