How do I open geotiff images with gdal in python?

◇◆丶佛笑我妖孽 提交于 2019-12-22 01:37:25

问题


I am trying to run the following code:

from osgeo import gdal
import sys

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

But I receive the following error:

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

Why does this happen?


回答1:


You have already opened the dataset, as Spacedman answered. GDAL is not a visualization library (at its core).

You can read the data with: data = src_ds.ReadAsArray()

And then pass it on the your favourite plotting library.

Alternatively you could simply output to a more common 'picture' format (PNG for example), and use any viewer you like to display the result. 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

The scaling is necessary to convert your data values to the 8-bit range (0-255) which commonly used for pictures.




回答2:


The following code opens a raster file and reads a band of the raster into a numpy array.

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



回答3:


You can do by following,

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


来源:https://stackoverflow.com/questions/41996079/how-do-i-open-geotiff-images-with-gdal-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!