1. 程式人生 > 其它 >rasterio實用教程(1)——影象讀寫

rasterio實用教程(1)——影象讀寫

技術標籤:Python影象處理pythonrasterio教程影象讀寫

文章目錄

背景

最近在做一些和影象處理相關的工作,涉及利用rasterio來實現遙感影像的鑲嵌、重取樣、重投影等,故開一個系列記錄下來。
rasterio是柵格影象處理的python包,依賴GDAL,如果你想了解如何二安裝GDAL,可以轉到我的另一篇博文

安裝

在GDAL安裝完成的基礎上,到這個網站下載對應版本的rasterio包,然後執行以下命令安裝whl檔案,即可安裝好rasterio。

pip install xxx.whl

簡介

rasterio的完整介面可以看

官網,這裡僅截圖展示部分api,如下圖所示。
在這裡插入圖片描述
可以看出,有clip(裁剪)、mask(掩膜)、merge(融合)、convert(轉換)等功能。

基操

影象讀取

import rasterio
tifPath = 'test.tif'
with rasterio.open(tifPath) as raster:
	data = raster.read(1)

這裡得到的data格式是np.array,表示柵格影象的第一層畫素數。raster物件還有很多其他屬性,例如bounds等。

影象寫入

dst_img = 'out.tif'# 輸出路徑
dataset = rasterio.open(
dst_img, 'w', driver='GTiff',# 影象型別 height=landuse.shape[0], width=landuse.shape[1], count=1,# 總層數 dtype=np.uint8,# 資料型別 crs=raster.crs,# 座標參考系 transform=raster.transform) # 轉換關係 dataset.write(landuse,1)# 寫入第一層 dataset.close()

這裡的landuse型別也是np.array嗎,注意資料格式和dtype要一致,否則會報錯。

展望

接下來會介紹影象鑲嵌、重取樣、影象對齊等操作。歡迎關注~