ee.Image.updateMask

Updates an image's mask at all positions where the existing mask is not zero. The output image retains the metadata and footprint of the input image.
UsageReturns
Image.updateMask(mask)Image
ArgumentTypeDetails
this: imageImageInput image.
maskImageNew mask for the image, as a floating-point value in the range 0, 1. If this image has a single band, it is used for all bands in the input image; otherwise, must have the same number of bands as the input image.

Examples

Code Editor (JavaScript)

// A Sentinel-2 surface reflectance image.
var img = ee.Image('COPERNICUS/S2_SR/20210109T185751_20210109T185931_T10SEG');
var trueColorViz = {
  bands: ['B4', 'B3', 'B2'],
  min: 0,
  max: 2700,
  gamma: 1.3
};
print('Sentinel-2 image', img);
Map.setCenter(-122.36, 37.47, 10);
Map.addLayer(img, trueColorViz, 'Sentinel-2 image');

// Create a Boolean land mask from the SWIR1 band; water is value 0, land is 1.
var landMask = img.select('B11').gt(100);
print('Land mask', landMask);
Map.addLayer(landMask, {palette: ['blue', 'lightgreen']}, 'Land mask');

// Apply the single-band land mask to all image bands; pixel values equal to 0
// in the mask become invalid in the image.
var imgMasked = img.updateMask(landMask);
print('Image, land only', imgMasked);
Map.addLayer(imgMasked, trueColorViz, 'Image, land only');

// Masks are band-specific. Here, a multi-band mask image is used to update
// corresponding input image band masks.
var imgBandSubset = img.select(['B4', 'B3', 'B2']);
var bandSpecificMasks = imgBandSubset.gt(200);
var imgBandSubsetMasked = imgBandSubset.updateMask(bandSpecificMasks);
print('Multi-band mask image', bandSpecificMasks);
print('Image, variable band masks', imgBandSubsetMasked);
Map.addLayer(bandSpecificMasks, null, 'Multi-band mask image');
Map.addLayer(imgBandSubsetMasked, trueColorViz, 'Image, variable band masks');
// Note that there is only a single alpha channel for visualization, so when
// the ee.Image is rendered as an RGB image or map tiles, a masked pixel in any
// band will result in transparency for all bands.

// Floating point mask values between 0 and 1 will be used to define opacity
// in visualization via Map.addLayer and ee.Image.visualize.
var landMaskFloat = landMask.add(0.65);
var imgMaskedFloat = img.updateMask(landMaskFloat);
print('Image, partially transparent', imgMaskedFloat);
Map.addLayer(imgMaskedFloat, trueColorViz, 'Image, partially transparent');