I'm creating a mosaic from many sentinel images with a bunch of overlapping areas, although I'm trying to understand what is criteria. For example:


This is a small piece totally clear of clouds


And this is a larger piece with many clouds


This is the result of the merge using rasterio and gdal

I used rasterio.merge and gdal_merge. Both brings me the same result, but don't understand the algorithm method yet. How can I tell the tool to preserve the image with fewer clouds as possible? Which method are these algorithms using to preserve the larger raster and not the cleanest (no clouds)?

It would be absolutely awesome if I could select the raster with fewer clouds to be merged for this mosaic. Is there a way to choose the raster I want to keep?

  • 1
    From the documentation gdal.org/programs/gdal_merge.html In areas of overlap, the last image will be copied over earlier ones.
    – user30184
    Dec 21, 2020 at 17:34
  • Thx @user30184 now I'm curious to understand the rasterio.merge method. There's no information at all on their documentation =/ Dec 22, 2020 at 14:36
  • 1
    The documentation points to ask questions here rasterio.groups.io/g/main
    – user30184
    Dec 22, 2020 at 14:42
  • You say there is no information in the rasterio docs but my answer shows that there is. Could you share how you searched? Maybe the docs need improvements. Dec 23, 2020 at 11:37

2 Answers 2


The documentation https://rasterio.readthedocs.io/en/latest/api/rasterio.merge.html says:

"Input files are merged in their listed order using the reverse painter’s algorithm (default) or another method."

And for the available methods predefined by rasterio:

first: reverse painting
last: paint valid new on top of existing
min: pixel-wise min of existing and new
max: pixel-wise max of existing and new

You can also write your own function, for example for a median or average of the files.

For your use case of removing clouds, you will probably not get good results from the predefined functions, a min for example would leave you without white clouds but with their dark shadows.


The order of inputs matters, give as an input first the cloudy image, then the not cloudy.

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.