You can do this in Python using NumPy by matching an image through a lookup table.
import numpy as np def map_uint16_to_uint8(img, lower_bound=None, upper_bound=None): ''' Map a 16-bit image trough a lookup table to convert it to 8-bit. Parameters ---------- img: numpy.ndarray[np.uint16] image that should be mapped lower_bound: int, optional lower bound of the range that should be mapped to ``[0, 255]``, value must be in the range ``[0, 65535]`` and smaller than `upper_bound` (defaults to ``numpy.min(img)``) upper_bound: int, optional upper bound of the range that should be mapped to ``[0, 255]``, value must be in the range ``[0, 65535]`` and larger than `lower_bound` (defaults to ``numpy.max(img)``) Returns ------- numpy.ndarray[uint8] ''' if not(0 <= lower_bound < 2**16) and lower_bound is not None: raise ValueError( '"lower_bound" must be in the range [0, 65535]') if not(0 <= upper_bound < 2**16) and upper_bound is not None: raise ValueError( '"upper_bound" must be in the range [0, 65535]') if lower_bound is None: lower_bound = np.min(img) if upper_bound is None: upper_bound = np.max(img) if lower_bound >= upper_bound: raise ValueError( '"lower_bound" must be smaller than "upper_bound"') lut = np.concatenate([ np.zeros(lower_bound, dtype=np.uint16), np.linspace(0, 255, upper_bound - lower_bound).astype(np.uint16), np.ones(2**16 - upper_bound, dtype=np.uint16) * 255 ]) return lut[img].astype(np.uint8)
source share