rudster
rudster

Reputation: 1377

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

try:
    tif = gdal.Open('filename.tif')
    tifArray = tif.ReadAsArray()
except:
    print 'The file does not exist.'
    sys.exit(0)

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?

Upvotes: 4

Views: 21854

Answers (1)

oz123
oz123

Reputation: 28878

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

import matplotlib.image as mpimg
import matplotlib.pyplot as plt

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)
plt.show()

band3 of marbles

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

Upvotes: 11

Related Questions