3

I am working with various file sets and one of the things I have not been able to figure out is how to get the compression used in the file that I opened.

The challenge is that I have some different data sources and I want to be able to address the way that I handle the file based upon if the source has compressed the data (lossy) or not.

Does anyone know how to get the compression type from the gdal dataset in python?

1
  • I suppose that it depends on the raster format. Which formats you may meet in your data?
    – user30184
    Commented Mar 16, 2018 at 21:30

1 Answer 1

8

Use the GDAL "IMAGE_STRUCTURE" metadata domain, i.e. dataset.GetMetadata('IMAGE_STRUCTURE')['COMPRESSION']

Example with and without compression:

from osgeo import gdal

dataset = gdal.OpenEx('int16_lzw.tif')
md = dataset.GetMetadata('IMAGE_STRUCTURE')

# Use dict.get method in case the metadata dict does not have a 'COMPRESSION' key
compression = md.get('COMPRESSION', None)

print(md)
# {'COMPRESSION': 'LZW', 'INTERLEAVE': 'BAND'}
print(compression)
# LZW

dataset = gdal.OpenEx('int16.tif') #Has no compression
md = dataset.GetMetadata('IMAGE_STRUCTURE')
compression = md.get('COMPRESSION', None)

print(md)
# {'INTERLEAVE': 'BAND'}
print(compression)
# None
1
  • 1
    Thanks Luke, In my case, the file doesn't actually have the 'COMPRESSION' tag as you suggested is possible. However what you suggested helped me to also find: dataset.GetMetadataDomainList() which allowed me to see and search out all of the possible values to read out.
    – A.A
    Commented Mar 19, 2018 at 8:38

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.