4

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kalemula aditya
  • 87
  • 1
  • 1
  • 4
  • Seems like "show" doesn't exist for this object. On [this](https://pcjericks.github.io/py-gdalogr-cookbook/raster_layers.html) page, they open a `*.tif` image and then `print gtif.GetMetadata()`. Does that work for you? – JonasVautherin Feb 02 '17 at 07:53
  • 2
    What are you expecting the `.show()` method to do? Draw the raster? Why? Where did you see documentation for this method? The `.Open` method *has* opened the raster - successfully. Now what do you want to do with it? – Spacedman Feb 02 '17 at 08:12
  • We need to read the images and to display the fused geotiff image in python. can you explain how to read an image in array – kalemula aditya Feb 02 '17 at 09:32

3 Answers3

9

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()
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Logan Byers
  • 1,454
  • 12
  • 19
  • @kalemulaaditya `ds` is `NoneType` because the file was not opened correctly. The most likely reason is the filename that is passed as a parameter to `gdal.Open` is incorrect or the absolute path needs to be given. – Logan Byers Feb 03 '17 at 14:17
7

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Rutger Kassies
  • 61,630
  • 17
  • 112
  • 97
3

You can do it by following.

from osgeo import gdal
gdal.UseExceptions()
ds = gdal.Open('Your Geotif image')
band = ds.getRasterBand(1)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • An explanation would be in order. E.g., what is the idea/gist? Please respond by [editing (changing) your answer](https://stackoverflow.com/posts/42856964/edit), not here in comments (***without*** "Edit:", "Update:", or similar - the answer should appear as if it was written today). – Peter Mortensen Aug 31 '21 at 13:58