def test_scale_dtype(): arr = np.array([0.0, 1.0]).astype(math_type) x = scale_dtype(arr, 'uint8') assert x.max() == 255 assert x.min() == 0 x = scale_dtype(arr, 'uint16') assert x.max() == 65535 assert x.min() == 0
def _postprocess( tile: numpy.ndarray, mask: numpy.ndarray, rescale: str = None, color_formula: str = None, ) -> Tuple[numpy.ndarray, numpy.ndarray]: """Post-process tile data.""" if rescale: rescale_arr = list(map(float, rescale.split(","))) rescale_arr = list(_chunks(rescale_arr, 2)) if len(rescale_arr) != tile.shape[0]: rescale_arr = ((rescale_arr[0]),) * tile.shape[0] for bdx in range(tile.shape[0]): tile[bdx] = numpy.where( mask, linear_rescale( tile[bdx], in_range=rescale_arr[bdx], out_range=[0, 255] ), 0, ) tile = tile.astype(numpy.uint8) if color_formula: # make sure one last time we don't have # negative value before applying color formula tile[tile < 0] = 0 for ops in parse_operations(color_formula): tile = scale_dtype(ops(to_math_type(tile)), numpy.uint8) return tile, mask
def post_process_tile( tile: numpy.ndarray, mask: numpy.ndarray, rescale: str = None, color_formula: str = None, ) -> Tuple[numpy.ndarray, numpy.ndarray]: """Tile data post processing.""" if rescale: rescale_arr = (tuple(map(float, rescale.split(","))), ) * tile.shape[0] for bdx in range(tile.shape[0]): tile[bdx] = numpy.where( mask, linear_rescale(tile[bdx], in_range=rescale_arr[bdx], out_range=[0, 255]), 0, ) tile = tile.astype(numpy.uint8) if color_formula: if issubclass(tile.dtype.type, numpy.floating): tile = tile.astype(numpy.int16) # make sure one last time we don't have # negative value before applying color formula tile[tile < 0] = 0 for ops in parse_operations(color_formula): tile = scale_dtype(ops(to_math_type(tile)), numpy.uint8) return tile
def _postprocess( tile: numpy.ndarray, mask: numpy.ndarray, tilesize: int, rescale: str = None, color_formula: str = None, ) -> Tuple[numpy.ndarray, numpy.ndarray]: if tile is None: # Return empty tile tile = numpy.zeros((1, tilesize, tilesize), dtype=numpy.uint8) mask = numpy.zeros((tilesize, tilesize), dtype=numpy.uint8) else: if rescale: rescale_arr = (tuple(map(float, rescale.split(","))), ) * tile.shape[0] for bdx in range(tile.shape[0]): tile[bdx] = numpy.where( mask, linear_rescale(tile[bdx], in_range=rescale_arr[bdx], out_range=[0, 255]), 0, ) tile = tile.astype(numpy.uint8) if color_formula: # make sure one last time we don't have # negative value before applying color formula tile[tile < 0] = 0 for ops in parse_operations(color_formula): tile = scale_dtype(ops(to_math_type(tile)), numpy.uint8) return tile, mask
def post_process( self, in_range: Optional[Tuple[NumType, NumType]] = None, out_dtype: Union[str, numpy.number] = "uint8", color_formula: Optional[str] = None, **kwargs: Any, ) -> "ImageData": """Post-process image data. Args: in_range (tuple): input min/max bounds value to rescale from. out_dtype (str, optional): output datatype after rescaling. Defaults to `uint8`. color_formula (str, optional): rio-color formula (see: https://github.com/mapbox/rio-color). kwargs (optional): keyword arguments to forward to `rio_tiler.utils.linear_rescale`. Returns: ImageData: new ImageData object with the updated data. Examples: >>> img.post_process(in_range=(0, 16000)) >>> img.post_process(color_formula="Gamma RGB 4.1") """ data = self.data.copy() mask = self.mask.copy() if in_range: rescale_arr = tuple(_chunks(in_range, 2)) if len(rescale_arr) != self.count: rescale_arr = ((rescale_arr[0]), ) * self.count for bdx in range(self.count): data[bdx] = numpy.where( self.mask, linear_rescale( data[bdx], in_range=rescale_arr[bdx], **kwargs, ), 0, ) data = data.astype(out_dtype) if color_formula: data[data < 0] = 0 for ops in parse_operations(color_formula): data = scale_dtype(ops(to_math_type(data)), numpy.uint8) return ImageData( data, mask, crs=self.crs, bounds=self.bounds, assets=self.assets, metadata=self.metadata, )
def _postprocess_tile(tile, mask, rescale=None, color_ops=None): """Tile data post-processing.""" if rescale: rescale = (tuple(map(float, rescale.split(","))), ) * tile.shape[0] for bdx in range(tile.shape[0]): tile[bdx] = numpy.where( mask, linear_rescale(tile[bdx], in_range=rescale[bdx], out_range=[0, 255]), 0, ) tile = tile.astype(numpy.uint8) if color_ops: # make sure one last time we don't have # negative value before applying color formula tile[tile < 0] = 0 for ops in parse_operations(color_ops): tile = scale_dtype(ops(to_math_type(tile)), numpy.uint8) return tile, mask
def _postprocess(tile, mask, tilesize, rescale=None, color_formula=None): if tile is None: # Return empty tile tile = numpy.zeros((1, tilesize, tilesize), dtype=numpy.uint8) mask = numpy.zeros((tilesize, tilesize), dtype=numpy.uint8) else: if rescale: rescale = (tuple(map(float, rescale.split(","))), ) * tile.shape[0] for bdx in range(tile.shape[0]): tile[bdx] = numpy.where( mask, linear_rescale(tile[bdx], in_range=rescale[bdx], out_range=[0, 255]), 0, ) tile = tile.astype(numpy.uint8) if color_formula: for ops in parse_operations(color_formula): tile = scale_dtype(ops(to_math_type(tile)), numpy.uint8) return tile, mask
def _apply_color_operations(self, img, color_ops): for ops in parse_operations(color_ops): img = scale_dtype(ops(to_math_type(img)), numpy.uint8) return img
def test_scale_round_trip(arr): x = to_math_type(arr) y = scale_dtype(x, arr.dtype) assert np.array_equal(arr, y)