3

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.

Vince
20.5k16 gold badges49 silver badges65 bronze badges
asked Jan 6, 2020 at 22:49
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 Commented Jan 6, 2020 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. Commented Jan 7, 2020 at 6:02
  • 1
    Here's some info from the expert: erouault.blogspot.com/search?q=python Commented Jan 7, 2020 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. Commented Jan 8, 2020 at 22:08

1 Answer 1

3

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.

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

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.