2

I'm looking to convert my GDAL commands to Python code using either GDAL or Rasterio, as I've heard this is better to use.

I've been scratching my head at this and I could be missing some simple stuff, but I've tried to check as much Python API documentation as possible, but I've been unable to find any for the potential complexity of my commands. They are;

gdal_translate -co COMPRESS=JPEG -co JPEG_QUALITY=90 -co PHOTOMETRIC=YCBCR -co TILED=YES -co BLOCKXSIZE=128 -co BLOCKYSIZE=128 uncompressed.tif compressed.tif

gdaladdo --config COMPRESS_OVERVIEW JPEG --config PHOTOMETRIC_OVERVIEW YCBCR --config INTERLEAVE_OVERVIEW PIXEL -r gauss compressed.tif 2 4 8 16 32 64 128 256 512 1024

With the aim being I'd like to be able to run scripts on many folders of TIFFs which will not be georeferenced.

It doesn't have to be using GDAL or Rasterio these are just the only Python modules I know that should be able to do the job.

4
  • 2
    If you want to directly convert your calls to use the GDAL translate/addo entry points, see the corresponding "options" objects that make it (a little) friendlier to specify all of the arguments – mikewatt Jan 6 '20 at 23:56
  • 3
    What's wrong with subprocess.Popen docs.python.org/3/library/subprocess.html to shell the commands that are already working? if it ain't broke don't try to fix it. If your TIFF files aren't georeferenced perhaps IView irfanview.com/faq.htm in batch mode might be more appropriate, I've used this tool since the early 2000's to convert unreferenced images. – Michael Stimson Jan 7 '20 at 6:02
  • 1
    Here's some info from the expert: erouault.blogspot.com/search?q=python – christoph Jan 7 '20 at 15:40
  • Thank you all for te above, I will start working through your links and suggestions. @Michael Stimson I have been considering the subprocess, but due to different gdal install locations on different machines it can get a little fiddley when calling - well for me. – JG_RS_GIS Jan 8 '20 at 22:08
2

I would do something like this:

co = [
    'COMPRESS=JPEG',
    'JPEG_QUALITY=90',
    'PHOTOMETRIC=YCBCR',
    'TILED=YES',
    'BLOCKXSIZE=128',
    'BLOCKYSIZE=128'
    ]
compressed_ds = gdal.Translate('compressed.tif', 'uncompressed.tif', creationOptions=co)

overviews = [2,4,8,16,32,64,128,256,512,1024]
compressed_ds.BuildOverviews('GAUSS', overviews)

compressed_ds = None

I'm not quite sure how to do the config options in gdaladdo, but this should give you a start at least.

2
  • This certainly has given me a start, I just need to look into the gdaladoo stuff, thank you! – JG_RS_GIS Jan 8 '20 at 22:39
  • This gave me more than a start, answered my questions, thanks again @Matsamentet – JG_RS_GIS Mar 3 '20 at 21:44

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.