Loic L.
Loic L.

Reputation: 407

Google Earth Engine - RGB image export from ImageCollection Python API

I encounter some problems with the Google Earth Engine python API to generate a RGB image based on an ImageCollection.

Basically to transform the ImageCollection into an Image, I apply a median reduction. After this reduction, I apply the visualize function where I need to define the different variables like the min and max. The problem is that these two values are image dependent.

dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')
        .filterBounds(ee.Geometry.Polygon([[39.05789266, 13.59051553],
                       [39.11335033, 13.59051553],
                       [39.11335033, 13.64477783],
                       [39.05789266, 13.64477783],
                       [39.05789266, 13.59051553]]))
        .filterDate('2016-01-01', '2016-12-31')
        .select(['B4', 'B3', 'B2'])

reduction = dataset.reduce('median')
            .visualize(bands=['B4_median', 'B3_median', 'B2_median'],
                         min=0,
                         max=3000,
                         gamma=1)

Thus for each different image I need to process these two values that can sightly change. Since the number of images I need to generate is huge, It is impossible to do that manually. I do not know how to overcome this problem and I cannot find any answer to that problem. An idea would be to find the minimal value of the image and the maximum value. But I did not find any function that allows to do that on the Javascript or python API.

I hope that someone will be able to help me.

Upvotes: 0

Views: 1445

Answers (1)

Kel Markert
Kel Markert

Reputation: 837

You can use img.reduceRegion() to get image statistics for the region you want and for each image to export. You will have to call the results of the region reduction into the visualization function. Here is an example:

geom = ee.Geometry.Polygon([[39.05789266, 13.59051553],
                   [39.11335033, 13.59051553],
                   [39.11335033, 13.64477783],
                   [39.05789266, 13.64477783],
                   [39.05789266, 13.59051553]])

dataset = ee.ImageCollection('LANDSAT/LC08/C01/T1_SR')\
    .filterBounds(geom)\
    .filterDate('2016-01-01', '2016-12-31')\
    .select(['B4', 'B3', 'B2'])

reduction = dataset.median()

stats = reduction.reduceRegion(reducer=ee.Reducer.minMax(),geometry=geom,scale=100,bestEffort=True)

statDict = stats.getInfo()

prettyImg = reduction.visualize(bands=['B4', 'B3', 'B2'],
                     min=[statDict['B4_min'],statDict['B3_min'],statDict['B2_min']]
                     max=[statDict['B4_max'],statDict['B3_max'],statDict['B2_max']],
                     gamma=1)

Using this approach, I get an output image like this:

I hope this helps!

Upvotes: 1

Related Questions