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.
-
2If 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 argumentsmikewatt– mikewatt2020年01月06日 23:56:38 +00:00Commented Jan 6, 2020 at 23:56
-
3What'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– Michael Stimson2020年01月07日 06:02:03 +00:00Commented Jan 7, 2020 at 6:02
-
1Here's some info from the expert: erouault.blogspot.com/search?q=pythonchristoph– christoph2020年01月07日 15:40:46 +00:00Commented 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.JG_RS_GIS– JG_RS_GIS2020年01月08日 22:08:54 +00:00Commented Jan 8, 2020 at 22:08
1 Answer 1
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.
-
This certainly has given me a start, I just need to look into the gdaladoo stuff, thank you!JG_RS_GIS– JG_RS_GIS2020年01月08日 22:39:33 +00:00Commented Jan 8, 2020 at 22:39
-
This gave me more than a start, answered my questions, thanks again @MatsamentetJG_RS_GIS– JG_RS_GIS2020年03月03日 21:44:45 +00:00Commented Mar 3, 2020 at 21:44
Explore related questions
See similar questions with these tags.