def load(self, **kwargs): '''Loads entire image into memory in a :class:`spectral.ImageArray`. Keyword Arguments: `dtype` (numpy.dtype): An optional dtype to which the loaded array should be cast. `scale` (bool, default True): Specifies whether any applicable scale factor should be applied to the data after loading. :class:`spectral.ImageArray` is derived from both :class:`spectral.Image` and :class:`numpy.ndarray` so it supports the full :class:`numpy.ndarray` interface. The returns object will have shape `(M,N,B)`, where `M`, `N`, and `B` are the numbers of rows, columns, and bands in the image. ''' import spectral from spectral.spectral import ImageArray from spectral.utilities.python23 import typecode from array import array import warnings from spectral.algorithms.spymath import has_nan, NaNValueWarning for k in list(kwargs.keys()): if k not in ('dtype', 'scale'): raise ValueError('Invalid keyword %s.' % str(k)) dtype = kwargs.get('dtype', ImageArray.format) data = array(typecode('b')) self.fid.seek(self.offset) data.fromfile(self.fid, self.nrows * self.ncols * self.nbands * self.sample_size) npArray = np.fromstring(data.tostring(), dtype=self.dtype) if self.interleave == spectral.BIL: npArray.shape = (self.nrows, self.nbands, self.ncols) npArray = npArray.transpose([0, 2, 1]) elif self.interleave == spectral.BSQ: npArray.shape = (self.nbands, self.nrows, self.ncols) npArray = npArray.transpose([1, 2, 0]) else: npArray.shape = (self.nrows, self.ncols, self.nbands) npArray = npArray.astype(dtype) if self.scale_factor != 1 and kwargs.get('scale', True): npArray = npArray / float(self.scale_factor) imarray = ImageArray(npArray, self) if has_nan(imarray): warnings.warn('Image data contains NaN values.', NaNValueWarning) return imarray
def load(self, **kwargs): '''Loads entire image into memory in a :class:`spectral.ImageArray`. Keyword Arguments: `dtype` (numpy.dtype): An optional dtype to which the loaded array should be cast. `scale` (bool, default True): Specifies whether any applicable scale factor should be applied to the data after loading. :class:`spectral.ImageArray` is derived from both :class:`spectral.Image` and :class:`numpy.ndarray` so it supports the full :class:`numpy.ndarray` interface. The returns object will have shape `(M,N,B)`, where `M`, `N`, and `B` are the numbers of rows, columns, and bands in the image. ''' import spectral from spectral.spectral import ImageArray from spectral.utilities.python23 import typecode from array import array import warnings from spectral.algorithms.spymath import has_nan, NaNValueWarning for k in list(kwargs.keys()): if k not in ('dtype', 'scale'): raise ValueError('Invalid keyword %s.' % str(k)) dtype = kwargs.get('dtype', ImageArray.format) data = array(typecode('b')) self.fid.seek(self.offset) data.fromfile(self.fid, self.nrows * self.ncols * self.nbands * self.sample_size) npArray = np.fromstring(data.tostring(), dtype=self.dtype) if self.interleave == spectral.BIL: npArray.shape = (self.nrows, self.nbands, self.ncols) npArray = npArray.transpose([0, 2, 1]) elif self.interleave == spectral.BSQ: npArray.shape = (self.nbands, self.nrows, self.ncols) npArray = npArray.transpose([1, 2, 0]) else: npArray.shape = (self.nrows, self.ncols, self.nbands) npArray = npArray.astype(dtype) if self.scale_factor != 1 and kwargs.get('scale', True): npArray = npArray / float(self.scale_factor) imarray = ImageArray(npArray, self) if has_nan(imarray): warnings.warn('Image data contains NaN values.', NaNValueWarning) return imarray
######################################################################### # # Send comments to: # Thomas Boggs, [email protected] # ''' Tools for handling files that are band interleaved by line (BIL). ''' from __future__ import division, print_function, unicode_literals import numpy as np from .spyfile import SpyFile, MemmapFile from spectral.utilities.python23 import typecode, tobytes, frombytes byte_typecode = typecode('b') class BilFile(SpyFile, MemmapFile): ''' A class to represent image files stored with bands interleaved by line. ''' def __init__(self, params, metadata=None): import spectral self.interleave = spectral.BIL if metadata is None: metadata = {} SpyFile.__init__(self, params, metadata) self._memmap = self._open_memmap('r')
# # Send comments to: # Thomas Boggs, [email protected] # ''' Tools for handling files that are band interleaved by pixel (BIP). ''' from __future__ import division, print_function, unicode_literals import numpy as np from .spyfile import SpyFile, MemmapFile from spectral.utilities.python23 import typecode byte_typecode = typecode('b') class BipFile(SpyFile, MemmapFile): ''' A class to interface image files stored with bands interleaved by pixel. ''' def __init__(self, params, metadata=None): import spectral self.interleave = spectral.BIP if metadata is None: metadata = {} SpyFile.__init__(self, params, metadata) self._memmap = self._open_memmap('r') def _open_memmap(self, mode):
def read_erdas_lan_header(fileName, byte_order=0): '''Read parameters from a lan file header. Arguments: fileName (str): File to open. byte_order (int) [default 0]: Specifies whether to read as little (0) or big (1) endian. ''' from array import array import sys import spectral from spectral.utilities.python23 import IS_PYTHON3, typecode if IS_PYTHON3: import builtins else: import __builtin__ as builtins f = builtins.open(fileName, "rb") h = {} h["format"] = "lan" h["fileName"] = fileName h["sizeOfHeader"] = 128 h["type"] = f.read(6) if h["type"] not in (b'HEAD74', b'HEADER'): raise IOError('Does not look like an ERDAS Lan header.') # Read all header data into arrays word = array(typecode('h')) dword = array(typecode('i')) float = array(typecode('f')) word.fromfile(f, 2) f.seek(16) if h["type"] == b'HEAD74': dword.fromfile(f, 4) else: float.fromfile(f, 4) f.seek(88) word.fromfile(f, 2) f.seek(106) word.fromfile(f, 1) float.fromfile(f, 5) if byte_order != spectral.byte_order: word.byteswap() dword.byteswap() float.byteswap() # Unpack all header data h["packing"] = word.pop(0) h["nbands"] = word.pop(0) if h["type"] == b'HEAD74': h["ncols"] = dword.pop(0) h["nrows"] = dword.pop(0) h["pixel_xcoord"] = dword.pop(0) h["pixel_ycoord"] = dword.pop(0) else: h["ncols"] = int(float.pop(0)) h["nrows"] = int(float.pop(0)) h["pixel_xcoord"] = float.pop(0) h["pixel_ycoord"] = float.pop(0) h["map_type"] = word.pop(0) h["nclasses"] = word.pop(0) h["area_unit"] = word.pop(0) h["ypixel_size"] = float.pop() h["xpixel_size"] = float.pop() h["map_ycoord"] = float.pop() h["map_xcoord"] = float.pop() h["narea_units"] = float.pop() f.close() return h
def read_erdas_lan_header(fileName, byte_order=0): '''Read parameters from a lan file header. Arguments: fileName (str): File to open. byte_order (int) [default 0]: Specifies whether to read as little (0) or big (1) endian. ''' from array import array import sys import spectral from spectral.utilities.python23 import IS_PYTHON3, typecode if IS_PYTHON3: import builtins else: import __builtin__ as builtins f = builtins.open(fileName, "rb") h = {} h["format"] = "lan" h["fileName"] = fileName h["sizeOfHeader"] = 128 h["type"] = f.read(6) if h["type"] not in (b'HEAD74', b'HEADER'): raise IOError('Does not look like an ERDAS Lan header.') # Read all header data into arrays word = array(typecode('h')) dword = array(typecode('i')) float = array(typecode('f')) word.fromfile(f, 2) f.seek(16) if h["type"] == b'HEAD74': dword.fromfile(f, 4) else: float.fromfile(f, 4) f.seek(88) word.fromfile(f, 2) f.seek(106) word.fromfile(f, 1) float.fromfile(f, 5) if byte_order != spectral.byte_order: word.byteswap() dword.byteswap() float.byteswap() # Unpack all header data h["packing"] = word.pop(0) h["nbands"] = word.pop(0) if h["type"] == b'HEAD74': h["ncols"] = dword.pop(0) h["nrows"] = dword.pop(0) h["pixel_xcoord"] = dword.pop(0) h["pixel_ycoord"] = dword.pop(0) else: h["ncols"] = int(float.pop(0)) h["nrows"] = int(float.pop(0)) h["pixel_xcoord"] = float.pop(0) h["pixel_ycoord"] = float.pop(0) h["map_type"] = word.pop(0) h["nclasses"] = word.pop(0) h["area_unit"] = word.pop(0) h["ypixel_size"] = float.pop() h["xpixel_size"] = float.pop() h["map_ycoord"] = float.pop() h["map_xcoord"] = float.pop() h["narea_units"] = float.pop() f.close() return h
# # Send comments to: # Thomas Boggs, [email protected] # """ Tools for handling files that are band interleaved by line (BIL). """ from __future__ import division, print_function, unicode_literals import numpy as np from .spyfile import SpyFile, MemmapFile from spectral.utilities.python23 import typecode byte_typecode = typecode("b") class BilFile(SpyFile, MemmapFile): """ A class to represent image files stored with bands interleaved by line. """ def __init__(self, params, metadata=None): import spectral self.interleave = spectral.BIL if metadata is None: metadata = {} SpyFile.__init__(self, params, metadata)