I am trying to replicate the gdal_translate command, but using pure Python:
gdal_translate infile.tif outfile.tif -co COMPRESS=JPEG -co PHOTOMETRIC=YCBCR -co TFW=YES -b 1 -b 2 -b 3
I have already looked at How to call gdal_translate from Python code? but am stuck with how to apply band information using the GDAL Python bindings.
So far I can see how to do all of this with the Python code, except apply the band (-b) settings.
#Import gdal from osgeo import gdal src_filename = r"infile.tif" dst_filename = r"outfile.tif" #Open existing dataset src_ds = gdal.Open( src_filename ) #Open output format driver, see gdal_translate --formats for list format = "GTiff" driver = gdal.GetDriverByName( format ) #Output to new format dst_ds = driver.CreateCopy( dst_filename, src_ds, 0, ['COMPRESS=JPEG','PHOTOMETRIC=YCBCR','TFW=YES']) #Properly close the datasets to flush to disk dst_ds = None src_ds = None
After reading the GDAL API Tutorial, it appears that I can only do this with the Create() method, not the CreateCopy() method. If I have to go the route of using the Create() method, what is the right approach to doing this? It looks like using a VRT is likely the right way, but I cannot wrap my head around how to write the code that would "make a copy" of the existing file while using either the Create() or VRT methods.