Reading TIFF in Python and Matplotlib using GDAL


I'm trying to display a grayscale TIFF file using Python and MatPlotLib,

So far I have read the file this:

import scipy as N
import gdal
import sys
import matplotlib.pyplot as pyplot

    tif = gdal.Open('filename.tif')
    tifArray = tif.ReadAsArray()
    print 'The file does not exist.'

band1 = tif.GetRasterBand(1)
band2 = tif.GetRasterBand(2)
band3 = tif.GetRasterBand(3)

band1Array = band1.ReadAsArray()
band2Array = band2.ReadAsArray()
band3Array = band3.ReadAsArray()

But then I don't know what else should I do... I'm so clueless. Anyone would help me in this?

4/29/2012 10:31:14 PM

Accepted Answer

Once you processed your file into a 2 Array, you could use ANY function in matplotlib that plots 2D arrays, e.g. cmap, imshow etc.

Here is the output with the marbles example

img=mpimg.imread('MARBLES.TIF ')
imgplot = plt.imshow(img)

Here is what you get if you view only band3 of the image:

imgplot2 = plt.imshow(band3Array)

band3 of marbles

Look further into image viewing in MPL and 2D array functions...

4/30/2012 5:43:53 PM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow