【问题标题】:How do I open geotiff images with GDAL in Python?如何在 Python 中使用 GDAL 打开 geotiff 图像?
【发布时间】:2017-02-02 07:06:48
【问题描述】:

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

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'

为什么会这样?

【问题讨论】:

  • 这个对象似乎不存在“show”。在this 页面上,他们打开*.tif 图像,然后打开print gtif.GetMetadata()。这对你有用吗?
  • 您希望.show() 方法做什么?绘制栅格?为什么?您在哪里看到此方法的文档? .Open 方法打开光栅 - 成功。现在你想用它做什么?
  • 我们需要读取图像并在python中显示融合的geotiff图像。你能解释一下如何读取数组中的图像

标签: python gdal geotiff


【解决方案1】:

以下代码打开一个光栅文件并将光栅的一个波段读入NumPy 数组。

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

【讨论】:

  • @kalemulaaditya dsNoneType,因为文件未正确打开。最可能的原因是作为参数传递给gdal.Open 的文件名不正确或需要提供绝对路径。
【解决方案2】:

您已经以Spacedman answered 的身份打开了数据集。 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) 所必需的。

【讨论】:

  • 错误是“针对 API 版本 9 编译的模块,但这个版本的 numpy 是 7”
【解决方案3】:

您可以按照以下方式进行。

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

【讨论】:

  • 解释一下。例如,想法/要点是什么?请通过editing (changing) your answer 回复,而不是在 cmets 中(without "Edit:"、"Update:" 或类似的 - 答案应该看起来像是今天写的)。
猜你喜欢
  • 2023-03-25
  • 1970-01-01
  • 1970-01-01
  • 2018-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-17
  • 2017-07-19
相关资源
最近更新 更多