4

I want to automate some processes in raster images. Thus, one of the modules that I use is gdal_merge module of GDAL library in order to create RGB and False color satellite images. So I execute:

gdal_merge.py -separate -n 0 -a_nodata 0 -o output.tif band_4.tif band_3.tif band_2.tif

which works fine. After that I want to render and save it with the current styling. Just like the option "save as rendered image" of QGIS. How can I succeed that with GDAL ?

4
  • Possible duplicate of gis.stackexchange.com/questions/48034/…. If not, please explain what styling you intend to save: RGB, paletted, pseudo-colour?
    – AndreJ
    Commented Aug 5, 2017 at 18:46
  • Yes, It is very similar to gis.stackexchange.com/questions/48034/… . Thank you for that @AndreJ. My general purpose is to create RGB and pseudo-colour 8bit images in order to serve via GeoServer of MapServer. I also try gdal_translate first to convert the bands into 8bits and after gdal_merge to create the 3-band image but it created an image with much noise in the pixels that represented the sea.
    – Capdi
    Commented Aug 6, 2017 at 13:13
  • It would help if you can tell more about your input rasters, maybe give a link to public access data.
    – AndreJ
    Commented Aug 6, 2017 at 13:18
  • My input rasters are Sentinel2 data (jp2) format. And I want to create 3-band.tif , 8bit images with GDAL. So at first I execute: gdal_merge.py -separate -n 0 -a_nodata 0 -co "PHOTOMETRIC=rgb" -o output.tif band_4.jp2 band_3.jp2 band_2.jp2 which creates a 3-band 16bit image. After that I run: gdal_translate -ot Byte -scale 0 65535 0 255 -b 1 -b 2 -b 3 output.tif output_8bit.tif but the result is an image with 0 values.When I run the above command after saving as rendered with QGIS the result is exactly what I expected
    – Capdi
    Commented Aug 6, 2017 at 13:49

1 Answer 1

4

Sentinel2 L1C values describe the TOA reflectency captured in 12 bit ranging from 0 to 1 multiplied with a fixed constant which is currently 10000.

So you don't have values from 0 to 65.535, rather from 0 to 10.000. Values for the ground even stay within the range from 0 to 4096 (maybe except some bright parts of glaciers).

QGIS applies value stretching automatically based on the histogram or std. deviation. You can see the values from the Layer properties:

enter image description here

So your picture will be way too dark if you scale down with a max value of 65535. If you want to keep the details in bright areas (e.g. clouds), you should scale from 0 to 10000. But if you want to have the ground bright enough, scale from 0 to 4096.

Additional hint: Instead of using gdal_merge, you can use gdalbuildvrt for the first step, which will be much faster.

gdalbuildvrt -separate -o output.vrt B04.jp2 B03.jp2 B02.jp2
gdal_translate -ot Byte -scale 0 4096 0 255 -b 1 -b 2 -b 3 output.vrt output_8bit.tif
1
  • I ran both gdalbuildvrt and gdal_merge with the scale of 0-4096 and works fine ! Thank you so much @RoVo !!
    – Capdi
    Commented Aug 7, 2017 at 9:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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