3

I am trying to export some images from the Earth Engine using Export.image.toDrive but it seems that all the files that I generate look corrupted.

When I mean corrupted, it means that if I open them with any viewer like Photoshop, Preview, etc, they look all black with lines (sample image at the bottom). Also I tried to open them with OpenCv in Python and the images did not have sense (they have three channels, but the number in the channels make no sense at all).

I looked for several examples online where people where exporting images and I couldn't find any difference with my code. Additionally, if I plot on the map of the Earth Engine the data seems to be fine. So I guess that I am doing something wrong with the exportation but I can't get it.

The procedure I follow is the following.

  1. I select an area of the map with the square tool.
  2. I run the script.
  3. I go to task and export some of the layers that appear there.
  4. I go to Drive and download the file to open it.
  5. File is corrupted :(.

code snippet

var s2 = ee.ImageCollection('COPERNICUS/S2') // Level-C1 (TOA), for Level-2A (BOA) use 'COPERNICUS/S2_SR'
  .filterBounds(geometry)
  .filter(ee.Filter.lessThanOrEquals('CLOUDY_PIXEL_PERCENTAGE', 50))
  .filter(ee.Filter.date('2020-01-01', '2020-01-10'))
  
var clip_all = function(img){
  return img.clip(geometry)
}

var s2_clip = s2.map(clip_all)

var s2_list = s2_clip.toList(s2_clip.size())

var n = s2_list.size().getInfo()

for (var i = 0; i < n; i++){
  var img = ee.Image(s2_list.get(i))
 
  var rgb = img.select('B2','B3', 'B4')//.float()
  var date = img.date().format('yyyyMMdd').getInfo()
  Map.addLayer(img, {bands: ['B4', 'B3', 'B2'], max: 5048}, 'Image_'+date)

  //export
  Export.image.toDrive({
  image: rgb,
  description: 'COPERNICUS_RGB'+ date,
  scale: 30,
  region: geometry, 
  maxPixels: 1e10,
  fileNamePrefix: 'exampleExport',
  folder: 'test_earthengine_2'
})
}

some variations that I tried:

  • using another scale 30-->10, the same.
  • adding float to the line var rgb = img.select('B2','B3', 'B4').float(), no changes.

I wonder if the problem is that the export can take really long time and the things that appear in my drive still need some processing? (I already waited for 8 hours and the files did not seem to change on my drive.)

When I mean corrupted this are the kind of images that I am obtaining:

corrupted image

4

Most likely, the image editing tools you are using are not expecting floating-point color channel (band) values, but integer. Floating-point color data is unusual outside of scientific (and sometimes game) computing, and many image editing or viewing programs do not support it.

You can use Image.visualize to convert the image to RGB, in the same way that Map.addLayer does for you implicitly.

Replace your line

  var rgb = img.select('B2','B3', 'B4')

with

  var rgb = img.visualize({bands: ['B4', 'B3', 'B2'], max: 5048});

Notice that these are exactly the same options you pass to Map.addLayer!

4
  • I can't believe that it was this simple. After this modification Everything works as expected. Any guideline or suggestion to choose the max argument? – Iván May 7 at 14:58
  • 1
    @Iván It's the same as with Map.addLayer: You need to pick it so that your values of interest aren't clipped to the brightest value (since integer values have a definite lower and upper bounds in the way floats don't). If you go to the layer list in the Code Editor and click on the gear icon for the layer, you can ask it to try to compute reasonable min and max values based on the observed range (the "Stretch" options), and then copy those numbers into your code. – Kevin Reid May 7 at 15:12
  • I see, great! Is there a way to algorithmically set by default the min max parameters so the fit the 100% stretch or a 1sigma stretch? – Iván May 7 at 15:41
  • 1
    @Iván Not automatically — you'll need a reduceRegion on the image with a combined mean and stdDev reducer, then calculate the range you want and pass those results to .visualize(). – Kevin Reid May 7 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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