I am quite new to working in the prompt for image analysis and data handling, so I realize this might very well be a very silly problem. In any occasion, I am using my tutor's code in order to radiometrically, atmospherically and topographically correct my images, which are in .tif format. I want to correct each band separately. So far the code looks like this:

    # import libraries

import numpy as np                               #numerical python

import matplotlib.pyplot as plt                  #plotting libraries
from pylab import *   
from matplotlib import cm  
from matplotlib.colors import ListedColormap

from spectral.io import envi                     #envi files library

import gdal                                      #Geospatial Data Abstraction 

wdir = 'E:/MOMUT1/Bansko_Sat_Img/OutputII/Ipython/copies/'

def readtiff ():
   filename1 = 'band1'
   info = envi.open(wdir+'band1')
   data = info.load()
   return data, info

[Wallow_TM,infoa]=readtiff(band1.tif)  # This load a Landsat 5 Thematic Mapper (TM) image

It is at this particular point that it returns a name error, saying "band1 is not define".

  • readtiff('band1.tif'). Next time please copy the actual exception message into your question.
    – user2856
    May 4, 2017 at 11:13

2 Answers 2


You seem to be using spectral python's class for ENVI format files, whilst it looks like you are trying to read a single band of a Landsat image as a TIFF file.

A TIFF can easily be loaded into a numpy array using matplotlib, so your function would become

import matplotlib.pyplot as plt
def readtiff(tiff_file):
    return, plt.imread(tiff_file)

# load the data from the file to a numpy array
filename = 'band1.tif'
data = readtiff(filename)

Note that matplotlib here will just be returning an ndarray of the image data and will not include any of the image metadata.

For this sort of image processing, you ought to be using the Geospatial Data Abstraction Library (GDAL) directly. There is an excellent tutorial on loading and processing raster imagery in python.

In this case, with GDAL you can get your image as a numpy array with:

from osgeo import gdal
import numpy as np
dataset = gdal.Open('filename.tif')
band = dataset.GetRasterBand(1)
data = band.ReadAsArray()
  • Hello, I thank you for your guidance. I tried with the code you provided, but errors kept coming out. Consequently I tried with the GDAL library guide, which you supplied and the code ran until it was time to run the atmospheric correction, where it produced a memory error. Looking through the forums I found that there is not basic solution for a memory error, because my machine is not equip in some way. Alternatively I think the root of the error must be in the inability to perform a conversion from a single to a double. I tried doing the conversion manually but still no avail
    – Momchill
    May 4, 2017 at 17:10
  • It is likely that the image is too large to load into memory - when you do ReadAsArray() the whole image is loaded into your computers RAM. There is not often a simple solution to this, but most commonly people would try on a computer with more RAM, or look into tiling the image. If you wanted to look into tiling there is a helper routine in the RSGISLib.
    – MBL
    May 5, 2017 at 9:02
  • Well, a lot of headaches later, everything runs until the actual radiometric and atmospheric correction starts (I am converting to radiance, then performing dark pixel subtraction, convert that to ToA and then to surface reflectance) when after due deliberation i get an index error, stating "too many indices for array". I understand that means my array is not built properly, but for the life of me, I can't figure out why. It's a 7-band stack from a Landsat TM-5 and it's a 3D array, reading the Z-dimension (the bands), the array is in dtype uint64. Any ideas? I would really appreciate some help
    – Momchill
    May 7, 2017 at 14:49

Well, the problem got resolved with due help from my tutor. Turns out the array in the code I was using was arranged: "Lon-Lat-Band", whereas the images I was using were arranged like: "Band-Lon-Lat", which produced the error. Switching the places where the column was in the list fixed the problem! Thanks to the whole community for the communal effort!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.