Skip to content

Instantly share code, notes, and snippets.

@ageller
Created November 19, 2021 20:21
Show Gist options
  • Save ageller/b3931de96247a181993610ac46942dc8 to your computer and use it in GitHub Desktop.
Save ageller/b3931de96247a181993610ac46942dc8 to your computer and use it in GitHub Desktop.
getSentinalS2SRImage block 3
# open the images
B2 = rasterio.open('B2.tif')
B3 = rasterio.open('B3.tif')
B4 = rasterio.open('B4.tif')
#get the scaling
image = np.array([B2.read(1), B3.read(1), B4.read(1)]).transpose(1,2,0)
p2, p98 = np.percentile(image, (2,98))
# use the B2 image as a starting point so that I keep the same parameters
B2_geo = B2.profile
B2_geo.update({'count': 3})
with rasterio.open(filename, 'w', **B2_geo) as dest:
dest.write( (np.clip(B4.read(1), p2, p98) - p2)/(p98 - p2)*255, 1)
dest.write( (np.clip(B3.read(1), p2, p98) - p2)/(p98 - p2)*255, 2)
dest.write( (np.clip(B2.read(1), p2, p98) - p2)/(p98 - p2)*255, 3)
B2.close()
B3.close()
B4.close()
# remove the intermediate files
for selection in bands:
os.remove(selection + '.tif')
os.remove(selection + '.zip')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment