def test_gamma(arr): x = gamma(arr, 0.95) assert x[0][0][0] - 0.033069782 < 1e-4 # output is not within the range of 0..1 with pytest.raises(ValueError): x = gamma(arr, -2.0) # test output contains inf and is out of range 0..1 with pytest.raises(ValueError): x = gamma(arr, -0.001) # test output contains NaN with pytest.raises(ValueError): x = gamma(arr, np.nan) with pytest.raises(ValueError): x = gamma(arr * -1, 2.2)
def execute(mp): """Enhance RGB colors from a Sentinel-2 SAFE archive.""" # read input SAFE file with mp.open( "input_file", resampling=mp.params["resampling"] ) as safe_file: try: # read red, green and blue bands & scale to 8 bit rgb = np.clip( safe_file.read( [4, 3, 2], mask_clouds=mp.params["mask_clouds"], mask_white_areas=mp.params["mask_white_areas"] ) / 16, 0, 255 ).astype("uint8") except MapcheteEmptyInputTile: return "empty" # scale to 0 to 1 for filters red, green, blue = rgb.astype("float") / 255 # save nodata mask for later & remove rgb to free memory mask = rgb.mask del rgb # using rio-color: # (1) apply gamma correction to each band individually # (2) add sigmoidal contrast & bias # (3) add saturation enhanced = np.clip( saturation( sigmoidal( np.stack([ # apply gamma correction for each band gamma(red, mp.params["red_gamma"]), gamma(green, mp.params["green_gamma"]), gamma(blue, mp.params["blue_gamma"]), ]), mp.params["sigmoidal_contrast"], mp.params["sigmoidal_bias"] ), mp.params["saturation"] ) * 255, # scale back to 8bit 1, 255 # clip valid values from 1 to 255 ) # use original nodata mask and return return np.where(mask, 0, enhanced).astype("uint8")
band = rasterio.band(dataset, 1) print(band) tile, mask = reader.tile(dataset, 852, 418, 10, tilesize=1024) min = 0 max = 60 tile1 = (tile[0]-min)/max*255 tile2 = (tile[1]-min)/max*255 tile3 = (tile[2]-min)/max*255 tileList = np.array([tile[0], tile[1], tile[2]]) renderData = np.where(tileList > 255, 255, tileList) renderData = np.where(renderData < 0, 0, renderData) renderData = renderData.astype(np.uint8) mtdata = to_math_type(renderData) data = gamma(mtdata, 1.7) data = sigmoidal(mtdata, 10, 0)*255 buffer = render(data.astype(np.uint8), mask=mask) with open("reraster2.png", "wb") as f: f.write(buffer)
def test_parse_multi_saturation_first(arr): f1, f2 = parse_operations("saturation 1.25 gamma rgb 0.95") assert np.array_equal(f2(f1(arr)), gamma(saturation(arr, 1.25), g=0.95))
def test_parse_comma(arr): # Commas are optional whitespace, treated like empty string f1, f2 = parse_operations("gamma r,g,b 0.95, sigmoidal r,g,b 35 0.13") assert np.array_equal( f2(f1(arr)), sigmoidal(gamma(arr, g=0.95), contrast=35, bias=0.13))
def test_parse_multi(arr): f1, f2 = parse_operations("gamma rgb 0.95 sigmoidal rgb 35 0.13") assert np.array_equal( f2(f1(arr)), sigmoidal(gamma(arr, g=0.95), contrast=35, bias=0.13))
def test_parse_gamma(arr): f = parse_operations("gamma rgb 0.95")[0] assert np.array_equal(f(arr), gamma(arr, 0.95))