You have left out details in your question, so I have to make some assumptions:
As you have JavaScript code here, you will not run this from Python, even though you want it to turn it into NumPy array. So you will need to export these results to either Cloud Storage or Drive.
You don't want the pixel values of the mask exported, but of the masked image.
You want to know the coordinate of each pixel
This involves running reduceRegion()
with a toList()
reducer. You will be quite limited in the number of pixels you can get this way. It's usually better to export this as an image if there's a larger number of pixels.
That said, below is an example how you can get the pixel values and export them as a CSV to Google Drive. Then you'll have to download that file and turn it into a NumPy array.
If you decide to write this in Python instead, you can use the same approach as here, but simply call getInfo()
on pixels
, and turn that dictionary into a NumPy array.
var masked = image
.addBands(ee.Image.pixelLonLat()) // Include lat/lng if you want
.updateMask(mask)
var pixels = masked
.reduceRegion({
reducer: ee.Reducer.toList(),
geometry: geometry,
scale: 100
})
var keys = ['someBand', 'longitude', 'latitude']
var features = ee.FeatureCollection(
pixels
.toArray(keys)
.transpose()
.toList()
.map(function (values) {
var properties = ee.Dictionary.fromLists(keys, values)
// Include the coordinate as GeoJSON, as lat/lon columns,
// or leave it out completely from the exported CSV.
// Here, both are included, which obviously is redundant.
var point = ee.Geometry.Point([
properties.get('longitude'),
properties.get('latitude')
])
return ee.Feature(point, properties)
})
)
Export.table.toDrive({
collection: features,
fileNamePrefix: 'pixels',
fileFormat: 'CSV',
// Select which properties to include, and the order of them
selectors: ['someBand', 'longitude', 'latitude', '.geo']
})
https://code.earthengine.google.com/9e6f714a30e91c236a7c7650beac95a0