pythoncompressiongdalrasterio

rasterio - set compression quality


I know about the possibility in the rasterio module to set a compression "family" like webp or JPEG by using the respective profile. But I want to have more control over the compression quality, like it is possible when using GDAL.

Ideally, I want something like this:

from osgeo import gdal

driver = gdal.GetDriverByName('GTiff')
ds = driver.Create(MYFILE, cols, rows, 1, dtype, ['COMPRESS=JPEG', 'JPEG_QUALITY=90'])

I could not find anything in the rasterio writing or profile documentation.


Solution

  • See the documentation here: https://rasterio.readthedocs.io/en/stable/topics/image_options.html#creation-options

    Basically the options need to be set when you open the dataset you will write your data to.

    So for your specific example it would be something like this:

    with rasterio.open("output.tif", "w", **src.meta, compress="JPEG",
                       jpeg_quality=90) as dataset:
        # Write data to the dataset.