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.open(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.open(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-12-06 09:21:00] - Read image ./data/demo/sentinel.tif
../_images/79d97272fe94acac1280e1444e1cc1b52f1d697b66e348ae602b40dcd7c62564.png ../_images/081f8a96ffca1d8d6a12b2eed685c1f94a84a6661b99f05264e9eb19fbf52b1d.png ../_images/394e0e66d03616143e0d9d048b7edbcda87782f01ad3d9b69c7ab4dc08c5d7ab.png ../_images/bcf24a9c9e477c71b53f95755f8178608397e8f114f98827aa204df3efb2756c.png ../_images/8a44d058b0b923e8955a922fd97deeeec1a8fc3f5211d90462d8b355a01002d0.png ../_images/3552688b696ff7ad76ce8b622f83502d83bd8c5978cf8267bff5745d28d23a5a.png ../_images/7350f19a0d0ead8c18545d8862464c298d0170cf3185b00fa861dc21b3fa51b0.png ../_images/909f0009febe342895a43ed7f8f72d008ed91afc60ca642edfe34b19a18f3f41.png ../_images/294e906bcc86a37937f3ea5d17a0656bab30119fc88b77b311a6519ff3625cb2.png ../_images/3aa51c0f025e22048074f3e0cfcc386bd68ee242838e0620f90aef85b4bc2fcf.png ../_images/6d496993ca10fc25362b072ca38d3b19f3eaeed200ef21ec444e6debd07b1349.png ../_images/cd32766ba64dc94ca177e562a704edb3685f4ed2982af78603b3245a642d83bd.png
help(image.add_band)
Help on method add_band in module rastereasy:

add_band(
    spectral_band,
    name_band=None,
    after_band=None,
    dtype=None,
    inplace=False,
    dest_name=None
) method of 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-12-06 09:21:00] - Read image ./data/demo/sentinel.tif
[2025-12-06 09:21:01] - Added band "13" at the end. Original bands: 12, New total: 13. Data type: int16.
../_images/79d97272fe94acac1280e1444e1cc1b52f1d697b66e348ae602b40dcd7c62564.png ../_images/081f8a96ffca1d8d6a12b2eed685c1f94a84a6661b99f05264e9eb19fbf52b1d.png ../_images/394e0e66d03616143e0d9d048b7edbcda87782f01ad3d9b69c7ab4dc08c5d7ab.png ../_images/bcf24a9c9e477c71b53f95755f8178608397e8f114f98827aa204df3efb2756c.png ../_images/8a44d058b0b923e8955a922fd97deeeec1a8fc3f5211d90462d8b355a01002d0.png ../_images/3552688b696ff7ad76ce8b622f83502d83bd8c5978cf8267bff5745d28d23a5a.png ../_images/7350f19a0d0ead8c18545d8862464c298d0170cf3185b00fa861dc21b3fa51b0.png ../_images/909f0009febe342895a43ed7f8f72d008ed91afc60ca642edfe34b19a18f3f41.png ../_images/294e906bcc86a37937f3ea5d17a0656bab30119fc88b77b311a6519ff3625cb2.png ../_images/3aa51c0f025e22048074f3e0cfcc386bd68ee242838e0620f90aef85b4bc2fcf.png ../_images/6d496993ca10fc25362b072ca38d3b19f3eaeed200ef21ec444e6debd07b1349.png ../_images/cd32766ba64dc94ca177e562a704edb3685f4ed2982af78603b3245a642d83bd.png ../_images/546798f19a10f66ec24d462878c212e51ad581629ea841b228c39daa54d81fe3.png