用于Numpy和Scipy的快速直接光栅I/O
rasterio的Python项目详细描述
示例
下面是Rasterio提供的一些基本功能的示例。三带 从图像中读取并取平均值以产生类似全色的东西 乐队。然后,这个新乐队将被写入一个新的单一乐队tiff。
importnumpyasnpimportrasterio# Read raster bands directly to Numpy arrays.#withrasterio.open('tests/data/RGB.byte.tif')assrc:r,g,b=src.read()# Combine arrays in place. Expecting that the sum will# temporarily exceed the 8-bit integer range, initialize it as# a 64-bit float (the numpy default) array. Adding other# arrays to it in-place converts those arrays "up" and# preserves the type of the total array.total=np.zeros(r.shape)forbandinr,g,b:total+=bandtotal/=3# Write the product as a raster band to a new 8-bit file. For# the new file's profile, we start with the meta attributes of# the source file, but then change the band count to 1, set the# dtype to uint8, and specify LZW compression.profile=src.profileprofile.update(dtype=rasterio.uint8,count=1,compress='lzw')withrasterio.open('example-total.tif','w',**profile)asdst:dst.write(total.astype(rasterio.uint8),1)
输出: