import rastereasy

Adding bands

The function add_band adds a band stored as a numpy table in the image. For stacking, it is better to use the stack function

Load images, change names (for better interpretation) and visualization of bands

name_im='./data/demo/sentinel.tif'
image=rastereasy.Geoimage(name_im,history=True)
names = {"NIR":8,"G":3,"CO" : 1,"SWIR2":11,"B": 2,"R":4,"RE1":5,"RE2":6,"RE3":7,"WA":9,"SWIR1":10,"SWIR3":12}
image_names=rastereasy.Geoimage(name_im,names=names,history=True)
image_names.info()
image_names.visu()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 12
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'CO': 1, 'B': 2, 'G': 3, 'R': 4, 'RE1': 5, 'RE2': 6, 'RE3': 7, 'NIR': 8, 'WA': 9, 'SWIR1': 10, 'SWIR2': 11, 'SWIR3': 12}

--- History of modifications---
[2025-10-17 13:55:14] - Read image ./data/demo/sentinel.tif
../_images/ad33dbecdc9aca524434261ca34af131c1e9b6ac1b8ee358fab021f708769a59.png ../_images/0b5fae2ac6af226547a668507e88e2244fbdec62ab8e6b9136763dacf03768c3.png ../_images/f77d841fdacdc836f16e214bd53a0cc2312bac0417e1220cf79ac81e4ed8e6d9.png ../_images/8b462cbf6107f25adaee11dfc23539a8f50504d481eae2b74e1a19dc1e337e77.png ../_images/0db1ad18cd60619e4a836d2c902a0410999c5741640b93804167187a9299cbb7.png ../_images/c71f9f271581230de1621b5d8c9ddeca6dc9fab29e5e66df29daed5b8c12ce5c.png ../_images/097504d195d94cf4d00b5ab60830f284b0f4e9c326fa9e75db3de2a019772d24.png ../_images/f9de79ff38fde2e62884a0fe20a661fac0341fa834f33f8bb3e249ad69766ea8.png ../_images/c6c67080399684c01ace57141a53aa1c48406892452f1a04d8018384918d46b7.png ../_images/82c88493b105fb5c7ec42ba76c24ff15475c9ffdf3c53c386f7b4b855fcb55df.png ../_images/0fa8ab40123991de55d24ae623acb8c80e4107585be7f41839b75b415bfa710e.png ../_images/8aa077cddbef62128225b7555698f11442d7a7fc31a996137384552f411228b8.png
help(image.add_band)
Help on method add_band in module rastereasy.rastereasy:

add_band(spectral_band, name_band=None, after_band=None, dtype=None, inplace=False, dest_name=None) method of rastereasy.rastereasy.Geoimage instance
    Add a new spectral band to the image.
    
    This method adds a new spectral band to the current image. The new band can
    be placed at the end of the band stack (default) or after a specified band.
    
    Parameters
    ----------
    spectral_band : numpy.ndarray
        The spectral band data to add. Can be in any of the following formats:
        - 2D array with shape (rows, cols)
        - 3D array with shape (1, rows, cols)
        - 3D array with shape (rows, cols, 1)
        The spatial dimensions must match the current image.
    
    name_band : str, optional
        Name to assign to the new band. If None, a sequential name will be used.
        Default is None.
    
    after_band : str, int, or None, optional
        Specify where to insert the new band:
        - If None: Add to the end of the band stack (default)
        - If str or int: Insert after the specified band name or index
        Default is None.
    
    dtype : str or None, optional
        Data type for the new band and resulting image. If None, preserves the
        highest precision type between the current image and the new band.
        Common values: 'float64', 'float32', 'int32', 'uint16', 'uint8'.
        Default is None.
    
    inplace : bool, default False
        If False, return a copy of the image with added band
        Otherwise, adding band in place and return None.
    
    dest_name : str, optional
        Path to save the updated image. If None, the image is not saved.
        Default is None.
    
    Returns
    -------
    Geoimage
        The modified image with the new band added or None if `inplace=True`.
    
    Raises
    ------
    ValueError
        If dimensions don't match, if the dtype is unknown, or if the after_band
        doesn't exist in the image.
    
    Examples
    --------
    >>> # Add a NDVI band to the end
    >>> ndvi = (image.select_band('NIR') - image.select_band('Red') / (image.select_band('NIR') + image.select_band('Red')
    >>> image_and_ndvi = image.add_band(ndvi, name_band='NDVI')
    >>> image_and_ndvi.info()  # Shows NDVI as the last band
    >>>
    >>> # Add a band after a specific position
    >>> image_and_ndvi = image.add_band(thermal_data, name_band='TIR', after_band='NIR')
    >>>
    >>> # Add with explicit data type and save
    >>> image.add_band(elevation, name_band='DEM', dtype='float32',inplace = True,
    >>>                dest_name='with_dem.tif')
    
    Notes
    -----
    - This method modifies the current image by adding a new band.
    - The spatial dimensions (rows, cols) of the new band must match the current image.

Add some bands

Two possibilities

  1. Return a new image with bands added

  2. Directly add bands from the image (option inplace=True)

1. Return a new image with bands removed

# 1. Select one band in an image
band_nir=image_names.select_bands('NIR')
# 2. Add this band to the image
image_added = image_names.add_band(band_nir.image)
image_added.info()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 13
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'CO': 1, 'B': 2, 'G': 3, 'R': 4, 'RE1': 5, 'RE2': 6, 'RE3': 7, 'NIR': 8, 'WA': 9, 'SWIR1': 10, 'SWIR2': 11, 'SWIR3': 12, '13': 13}

2. Directly add bands from the image (option inplace=True)

image_names.add_band(band_nir.image,inplace=True)
image_names.info()
image_names.visu()
- Size of the image:
   - Rows (height): 1000
   - Cols (width): 1000
   - Bands: 13
- Spatial resolution: 10.0  meters / degree (depending on projection system)
- Central point latitude - longitude coordinates: (7.04099599, 38.39058840)
- Driver: GTiff
- Data type: int16
- Projection system: EPSG:32637
- Nodata: -32768.0

- Given names for spectral bands: 
   {'CO': 1, 'B': 2, 'G': 3, 'R': 4, 'RE1': 5, 'RE2': 6, 'RE3': 7, 'NIR': 8, 'WA': 9, 'SWIR1': 10, 'SWIR2': 11, 'SWIR3': 12, '13': 13}

--- History of modifications---
[2025-10-17 13:55:14] - Read image ./data/demo/sentinel.tif
[2025-10-17 13:55:15] - Added band "13" at the end. Original bands: 12, New total: 13. Data type: int16.
../_images/ad33dbecdc9aca524434261ca34af131c1e9b6ac1b8ee358fab021f708769a59.png ../_images/0b5fae2ac6af226547a668507e88e2244fbdec62ab8e6b9136763dacf03768c3.png ../_images/f77d841fdacdc836f16e214bd53a0cc2312bac0417e1220cf79ac81e4ed8e6d9.png ../_images/8b462cbf6107f25adaee11dfc23539a8f50504d481eae2b74e1a19dc1e337e77.png ../_images/0db1ad18cd60619e4a836d2c902a0410999c5741640b93804167187a9299cbb7.png ../_images/c71f9f271581230de1621b5d8c9ddeca6dc9fab29e5e66df29daed5b8c12ce5c.png ../_images/097504d195d94cf4d00b5ab60830f284b0f4e9c326fa9e75db3de2a019772d24.png ../_images/f9de79ff38fde2e62884a0fe20a661fac0341fa834f33f8bb3e249ad69766ea8.png ../_images/c6c67080399684c01ace57141a53aa1c48406892452f1a04d8018384918d46b7.png ../_images/82c88493b105fb5c7ec42ba76c24ff15475c9ffdf3c53c386f7b4b855fcb55df.png ../_images/0fa8ab40123991de55d24ae623acb8c80e4107585be7f41839b75b415bfa710e.png ../_images/8aa077cddbef62128225b7555698f11442d7a7fc31a996137384552f411228b8.png ../_images/abeb73fb17d906ccbb74b94688c205f186096f089b55b610f49f5095831bd429.png