In Google Earth Engine's Java API, I have the following code that works and am trying to use this in Google Colab with Python.

How do you reclassify the pixels in a raster image in Python?

var FinalResult= datasetA.where(sdA.gt(sdAthreshold),56).where(sdA.lte(sdAthreshold).and(mA.gte(All_A)),102).where(sdA.lte(sdAthreshold).and(mA.lt(ALL_A)),76).rename('Result');

I have tried the following after reading NumPy reclassification documentation to no avail. This did not work.

import numpy as np
FinalResult= np.select.datasetA([sdA > sdAthreshold, (sdA <= sdAthreshold) & (mA >= AllA), (sdA <= sdAthreshold) & (mA < AllA)],[56,102,76])

And neither did this.

datasetA[np.where(sdA > sdAthreshold)] = 1 
datasetA[np.where((sdA <= sdAthreshold) & (mA >= AllA))] = 2 
datasetA[np.where((sdA <= sdAthreshold) & (mA < AllA))] = 3 
  • Could you please explain what you want to do? I don't know aboud earth engine but I'm good at python so I may be able to help you if you explain what you mean by reclassifying.
    – Parsa
    Aug 9, 2021 at 22:39
  • Thank you! I have a raster image collection, 10 years of data (datasetA). I want to classify the land area (each 30x30m pixel within my area of interest) in the raster collection as '76', '101' or '56' based on the standard deviation of a pixel across those 10 years and the mean of all pixels within a single year (single raster in the collection). For example, if the standard deviation of a pixel across the 10 yrs of data is greater than X, and the mean of the pixel across the 10 yrs of data is more often greater than the mean of pixels within a year, I want the pixel to be labeled 101, etc.
    – Megan
    Aug 10, 2021 at 12:23
  • ok thanks I think I got what you want. what are the shapes of your arrays datasetA, sdA, sdAthreshold, mA and AllA? providing this I can explain what you want. by the way I post an answer, if it didn't work, tell me the shapes and I will try to give a proper answer.
    – Parsa
    Aug 10, 2021 at 13:28
  • datasetA is an image collection of Landsat 8 composite NDVI, sdA is the standard deviation of datasetA (sdA = datasetA.reduce(ee.Reducer.stdDev()); ), the sdAthreshold is just a constant value (20), mA is the mean of datasetA (mA = datasetA.reduce(ee.Reducer.mean()); ), AllA is the average of all the pixels across all years.
    – Megan
    Aug 10, 2021 at 13:35
  • yeah but I meant the shapes. for example sdA.shape if it's a numpy array. it should be a tuple for exmaple (1000,1000, 3)
    – Parsa
    Aug 10, 2021 at 13:40

1 Answer 1


If your variables where numpy arrays, you could simply do this:

datasetA[sdA > sdAthreshold] = 1 
datasetA[np.logical_and(sdA <= sdAthreshold, mA >= AllA)] = 2 
datasetA[np.logical_and(sdA <= sdAthreshold, mA < AllA)] = 3

But in earth engine, your variables aren't numpy arrays, so numpy will not help you here.

EaarthEngine API is mostly similar in JavaScript and Python. So you can use something similar to what you provided in your question in JS. here is what I tried and it didn't raise any errors. Give it a try and see if this is what you were seeking.

FinalResult = sdG.where(sdG.gt(sdGthreshold),56) \
                 .where(sdG.lte(sdGthreshold).And(mG.gte(CLallyearsG)),102) \
                 .where(sdG.lte(sdGthreshold).And(mG.lt(CLallyearsG)),76) \
  • This results in an error: File "<ipython-input-22-57886e22cebf>", line 8 datasetA[np.logical_and(sdA <= sdAthreshold, mA >= AllA] = 2 ^ SyntaxError: invalid syntax
    – Megan
    Aug 10, 2021 at 13:43
  • yeah I forgot to close the parentheses :D
    – Parsa
    Aug 10, 2021 at 13:44
  • Ah, thanks! Now the error is: "TypeError: '>' not supported between instances of 'Image' and 'int'"
    – Megan
    Aug 10, 2021 at 13:45
  • I'll give it a look.
    – Parsa
    Aug 10, 2021 at 13:49
  • I think I found out. I edited the asnwer.
    – Parsa
    Aug 10, 2021 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.