Add a suffix or a prefix to bands


In [1]:
import ee
ee.Initialize()

In [2]:
from geetools import tools

In [3]:
import ipygee as ui

In [4]:
i = ee.Image('COPERNICUS/S2/20181122T142749_20181122T143353_T18GYT').select('B.')

In [5]:
ui.eprint(i.bandNames())


add a suffix


In [6]:
suffix = '_suffix'

In [7]:
renamed_s = tools.image.addSuffix(i, suffix, ['B2', 'B4', 'B8'])

In [8]:
ui.eprint(renamed_s.bandNames())


add prefix


In [9]:
prefix = 'prefix_'

In [10]:
renamed_p = tools.image.addPrefix(i, prefix, ['B2', 'B4', 'B8'])

In [11]:
ui.eprint(renamed_p.bandNames())


In a collection


In [12]:
col = ee.ImageCollection('COPERNICUS/S2').limit(5)

In [13]:
def print_bands(col):
    info = col.getInfo()
    images = info['features']
    for image in images:
        bands = image['bands']
        print([band['id'] for band in bands])

In [14]:
print_bands(col)


['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']

In [15]:
renamed_col_s = col.map(lambda img: tools.image.addSuffix(img, suffix, ['B2', 'B4', 'B8']))

In [16]:
print_bands(renamed_col_s)


['B1', 'B2_suffix', 'B3', 'B4_suffix', 'B5', 'B6', 'B7', 'B8_suffix', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2_suffix', 'B3', 'B4_suffix', 'B5', 'B6', 'B7', 'B8_suffix', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2_suffix', 'B3', 'B4_suffix', 'B5', 'B6', 'B7', 'B8_suffix', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2_suffix', 'B3', 'B4_suffix', 'B5', 'B6', 'B7', 'B8_suffix', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']
['B1', 'B2_suffix', 'B3', 'B4_suffix', 'B5', 'B6', 'B7', 'B8_suffix', 'B8A', 'B9', 'B10', 'B11', 'B12', 'QA10', 'QA20', 'QA60']

In [ ]: