spectralpython / spectral

Python module for hyperspectral image processing
MIT License
573 stars 139 forks source link

envi.save_image() fails when writing a single band numpy array imported by SPy_obj.read_band() #20

Closed MoTSCHIGGE closed 9 years ago

MoTSCHIGGE commented 9 years ago

Version 0.16.1: I imported a single band like this:

import spectral
Obj      = spectral.open_image('image.hdr')
Band1 = Obj.read_band(0)

When I try to save the imported 2D numpy array "Band1 using envi.save_saveimage with interleave ='bsq', I get the following error:

File "...\Python27\lib\site-packages\spectral\io\envi.py", line 537, in _prepared_data_and_metadata
  data = data.transpose(interleave_transpose(src_interleave, interleave))

ValueError: axes don't match array

The problem is that read_band() returns a 2D, not a 3D numpy array, which cannot be transposed with the argument (2,0,1), which is returned by spectral.io.spyfile.interleave_transpose('bip','bsq'). In my case I try to do this:

data = np_array_of_shape_10_10.transpose(2,0,1)
ValueError: axes don't match array

Solution: The 2D numpy array returned by Obj.read_band(0) has to be converted into a 3D array like this: Band1 = Obj.read_band(0) Band1 = Band1[ : , : , numpy.newaxis]

Afterwards it can be saved by envi.save_image @tboggs Would it be possible modify the save_image function to be compatible with 2D numpy arrays?