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

2024-06-07 07:24:09 发布

您现在位置: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'

为什么会这样?


Tags: to代码fromimportsrcshowsysds
3条回答

你可以跟着做

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

下面的代码打开一个光栅文件,并将光栅的一个波段读取到numpy数组中。

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

正如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)所必需的。

相关问题 更多 >