1

I have a true color image raster (bands red, green, blue of Sentinel-2) that I want to save with a certain style: enter image description here

In QGIS when you are exporting a raster you have two output modes: enter image description here

"Raw data" and "Rendered image".

The Rendered Image option saves the raster with the layers current styling. I want to do the same using GDAL or even rasterio, but I haven't been able to figure it out how to preserve the styling of the layer.

Does anyone know what code QGIS is using for this?

3
  • I do not know what code QGIS is using, but you can see what parameters it is using in rendering through Layer - Copy style. Paste the output into text editor, it is XML.
    – user30184
    Commented Apr 23 at 13:18
  • This is not what I am looking for. I need to know how to save the raster with the current styling since not all rasters will be the same @user30184
    – rrwork___
    Commented Apr 24 at 8:41
  • 1
    What you have is the original image and the QGIS styles which give the recipe for changing the visual look on-the-fly. GDAL can do something similar through the VRT file. Colors can be adjusted with a look-up table and contrast enhancement can be done with scaling gdal.org/drivers/raster/vrt.html#complexsource. Before you can re-produce the QGIS styling with GDAL or any other program you need to know how QGIS adjusts the image and that information you can get from the style XML. For simple contrast stretch it might be enough to check the min/max values from Symbology - Band rendering.
    – user30184
    Commented Apr 24 at 9:01

1 Answer 1

1

Well I figured a way to do it, even though the output of the code is not exactly the same. I try to imitate the same thing qgis does when you add a raster. But I created a function that did the cumulative count cut as qgis, using the np.nanpercentile between 2% and 98%.

I then converted the image from integer to byte so the scale is between 0-255. From that I saved the image using gdal,and made sure to add an alpha band.

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.