如何在python中使用gdal打开geotiff图像?

时间:2017-02-02 07:06:48

标签: python gdal geotiff

我正在尝试运行以下代码:

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'

为什么会这样?

3 个答案:

答案 0 :(得分:6)

你已经打开了数据集,正如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)所必需的。

答案 1 :(得分:5)

以下代码打开一个光栅文件,并将光栅的一个带读入一个numpy数组。

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

答案 2 :(得分:3)

你可以通过以下方式来做,

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