reading .img envi file with spectral python

7.8k Views Asked by At

I am trying to open an envi .img file and there is a .hdr file having same name. In the .img file there are two images which I can read using following code.

from spectral import *
img = open_image('LC08_L1TP_029029_20130330_20170310_01_T1_sensor_B05.img')

And the properties of img(BSQ file) is as following figure

In[352] img

Out[352]: 

Data Source:   '.\LC08_L1TP_029029_20130330_20170310_01_T1_sensor_B05.img'
# Rows:           7311
# Samples:        7371
# Bands:             2
Interleave:        BSQ
Quantization:  16 bits
Data format:     int16

What I want to extract those two images from img. But when I am trying with

img[:,:,1]

and it gives me an array of size(7311,7371,1) but all the values inside the array is zero but I know they should be non zero values.

My question is how can I extract those two images from the BSQ file?

2

There are 2 best solutions below

0
On

You can read the envi image from the hdr file having the same name.

import numpy as np
from spectral import*    
img1=open_image("<path to file.hdr>").read_band(0)
img2=open_image("<path to file.hdr>").read_band(1)

now you have extracted bith bands to img1 and img2, and you can save them or display them as per your discretion.

0
On

You can try this variant:

from spectral import * 

img = open_image('LC08_L1TP_029029_20130330_20170310_01_T1_sensor_B05.img') 
img_open = img.open_memmap(writeable = True) 
img_band = img_open[:,:,1] 
envi.save_image('new_image.bsq', ext='bsq', interleave = 'BSQ')

or

This variant needs open image by hdr file. But it should working like previous variant.

from spectral import *
img = envi.open('<name of hdr file>') 
img_open = img.open_memmap(writeable = True) 
img_band = img_open[:,:,1] 
envi.save_image('new_image.bsq', ext='bsq', interleave = 'BSQ')