Search code examples
pythonfor-loopgoogle-colaboratorynested-listslibgee

For loop over 'ee.image.Image' with 'ee.ee_list.List' of bands with Earth Engine Python API in Colab


I want to use a function to scale the bands of an image. The number and name of the image bands may differ. The following (working) code almost does what I want, except I want to loop over the (number of) bandnames, instead of a pre-defined range size:

from scipy.stats.distributions import randint_gen
# Function to scale the images (one mosaicked ee.Image) to a Uint8 format scaled from 0-255
def scale_composites(image):

  image_copy = image
  bandnames = image.bandNames()
  image = image.select(0).multiply(0).rename('base')
  min_max = [-5,5] # Min and Max values for S1 change ratios (list)

  bandlist = []
   for band in range(0, 4):
     scaledband = image_copy.select(band).unitScalemin_max[0], min_max[1]).multiply(255).toUint8()
     bandlist.append(scaledband)

  image = image.addBands(bandlist)
  image = image.select(bandnames)

  return image

The input of the function is an 'ee.image.Image', and 'bandnames' is an 'ee.ee_list.List'.

I have tried double loops with enumerate, length, toList, ... but I cannot figure out how to adapt the loop to my purpose.


Solution

  • We can get the band names as a list by using getInfo():

    ee_object = ee.Image('JAXA/ALOS/AW3D30/V2_2')
    bandnames = ee_object.bandNames().getInfo()
    

    and we don't need a for loop to iterate over bands as select can take a list (of band names) as an argument:

    scaledbands = ee_object.select(bandnames).unitScale(min_max[0], min_max[1]).multiply(255).toUint8()
    new_ee_object = ee_object.addBands(scaledbands)
    

    print(new_ee_object) outputs:

    ee.Image({
      "functionInvocationValue": {
        "functionName": "Image.addBands",
        "arguments": {
          "dstImg": {
            "functionInvocationValue": {
              "functionName": "Image.load",
              "arguments": {
                "id": {
                  "constantValue": "JAXA/ALOS/AW3D30/V2_2"
                }
              }
            }
          },
          "srcImg": {
            "functionInvocationValue": {
              "functionName": "Image.toUint8",
              "arguments": {
                "value": {
                  "functionInvocationValue": {
                    "functionName": "Image.multiply",
                    "arguments": {
                      "image1": {
                        "functionInvocationValue": {
                          "functionName": "Image.unitScale",
                          "arguments": {
                            "high": {
                              "constantValue": 5
                            },
                            "input": {
                              "functionInvocationValue": {
                                "functionName": "Image.select",
                                "arguments": {
                                  "bandSelectors": {
                                    "constantValue": [
                                      "AVE_DSM",
                                      "AVE_STK",
                                      "AVE_MSK"
                                    ]
                                  },
                                  "input": {
                                    "functionInvocationValue": {
                                      "functionName": "Image.load",
                                      "arguments": {
                                        "id": {
                                          "constantValue": "JAXA/ALOS/AW3D30/V2_2"
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            },
                            "low": {
                              "constantValue": -5
                            }
                          }
                        }
                      },
                      "image2": {
                        "functionInvocationValue": {
                          "functionName": "Image.constant",
                          "arguments": {
                            "value": {
                              "constantValue": 255
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    })