title: Export Google Earth Engine RGB Sentinel-2 Imagery to Google Drive using Python API
body: <p>This post isn't a question but a solution to a problem I have been trying to solve for a while. Hopefully somebody else will find the code useful!</p>
  
  <p>I wanted to export Sentinel-2 Satellite imagery (<a href="https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2" rel="noreferrer">https://developers.google.com/earth-engine/datasets/catalog/COPERNICUS_S2</a>) with a cloud masking filter applied from Google Earth Engine to my Google Drive using the Python API. However, not all images fully overlapped with the geometry I was interested in and the cloud mask made parts of some images invisible. I therefore needed to create a mosaic of the images closest to the date I was interested.</p>
  
  <p>The solution which eventually worked is below:</p>
  
  <pre><code># This is the cloud masking function provided by GEE but adapted for use in Python.
  def maskS2clouds(image):
      qa = image.select('QA60')
  
      # Bits 10 and 11 are clouds and cirrus, respectively.
      cloudBitMask = 1 &lt;&lt; 10
      cirrusBitMask = 1 &lt;&lt; 11
  
      # Both flags should be set to zero, indicating clear conditions.
      mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      mask = mask.bitwiseAnd(cirrusBitMask).eq(0)
  
      return image.updateMask(mask).divide(10000)
  
  
  # Define the geometry of the area for which you would like images.
  geom = ee.Geometry.Polygon([[33.8777, -13.4055],
                              [33.8777, -13.3157],
                              [33.9701, -13.3157],
                              [33.9701, -13.4055]])
  
  # Call collection of satellite images.
  collection = (ee.ImageCollection("COPERNICUS/S2")
                # Select the Red, Green and Blue image bands, as well as the cloud masking layer.
                .select(['B4', 'B3', 'B2', 'QA60'])
                # Filter for images within a given date range.
                .filter(ee.Filter.date('2017-01-01', '2017-03-31'))
                # Filter for images that overlap with the assigned geometry.
                .filterBounds(geom)
                # Filter for images that have less then 20% cloud coverage.
                .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))
                # Apply cloud mask.
                .map(maskS2clouds)
               )
  
  # Sort images in the collection by index (which is equivalent to sorting by date), 
  # with the oldest images at the front of the collection.
  # Convert collection into a single image mosaic where only images at the top of the collection are visible.
  image = collection.sort('system:index', opt_ascending=False).mosaic()
  
  
  # Assign visualization parameters to the image.
  image = image.visualize(bands=['B4', 'B3', 'B2'],
                          min=[0.0, 0.0, 0.0],
                          max=[0.3, 0.3, 0.3]
                         )
  
  # Assign export parameters.
  task_config = {
      'region': geom.coordinates().getInfo(),
      'folder': 'Example_Folder_Name',
      'scale': 10,
      'crs': 'EPSG:4326',
      'description': 'Example_File_Name'
  }
  
  # Export Image
  task = ee.batch.Export.image.toDrive(image, **task_config)
  task.start()
  </code></pre>