簡體   English   中英

如何在python中使用gdal打開geotiff圖像?

[英]How do I open geotiff images with gdal in python?

我正在嘗試運行以下代碼:

from osgeo import gdal
import sys

# this allows GDAL to throw Python Exceptions
src_ds = gdal.Open( "fused.tif" )
src_ds.show()

但是我收到以下錯誤:

Traceback (most recent call last):
    File ".../gdalopen1.py", line 5, in module src_ds.show()
AttributeError: 'Dataset' object has no attribute 'show'

為什么會這樣?

正如Spacedman回答的那樣,您已經打開了數據集。 GDAL不是可視化庫(其核心)。

您可以使用以下方式讀取數據: data = src_ds.ReadAsArray()

然后將其傳遞到您喜歡的繪圖庫中。

或者,您可以簡單地輸出為更常見的“圖片”格式(例如PNG),然后使用您喜歡的任何查看器來顯示結果。 vmin = 0 # minimum value in your data (will be black in the output) vmax = 1 # minimum value in your data (will be white in the output) ds = gdal.Translate('fused.png', 'fused.tif', format='PNG', outputType=gdal.GDT_Byte, scaleParams=[[vmin,vmax]]) ds = None

必須進行縮放,才能將數據值轉換為圖片常用的8位范圍(0-255)。

以下代碼打開一個柵格文件,並將柵格的條帶讀取到numpy數組中。

from osgeo import gdal
ds = gdal.Open('input.tif', gdal.GA_ReadOnly)
rb = ds.GetRasterBand(1)
img_array = rb.ReadAsArray()

您可以按照以下步驟進行操作:

from osgeo import gdal
gdal.UseExceptions()
ds=gdal.Open('Your Geotif image') 
band= ds.getRasterBand(1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM