2

I'm trying to use gdal.translate() to convert a raster from unit16 to int16 and I am trying to figure out what arguments to use.

I think in the command prompt version I would use -ot Int16 but that isn't working in python?

The documentation on the gdal website is for the command prompt and not to use in python. How do I know how to convert these argmunets so they work in python? I've come across this problem a few times while using gdal but was lucky enough this time to find an example of what I wanted to do. This time not so much.

Here is what I have so far. It then gives the warning: "Warning 6: driver GTiff does not support creation option outputType"

co = [
        'outputType=Int16'
        'noData=-10000'
    ]

# save as a new raster that is 16bit SIGNED to allow for -10000 value
print("copying to new raster" + newimage)
gdal.Translate(newimage, image, creationOptions=co)
1

1 Answer 1

8

You have two options here:

Call the gdal_translate command line utility using Python:

import subprocess

subprocess.call('gdal_translate -of GTiff -ot Int16 input.tif output.tif')

Or use gdal.Translate(). As you noted, options names differ from the command line utility and they can be found here.

TranslateOptions(options=[], format=None, outputType=GDT_Unknown, bandList=None, maskBand=None, width=0, height=0, widthPct=0.0, heightPct=0.0, xRes=0.0, yRes=0.0, creationOptions=None, srcWin=None, projWin=None, projWinSRS=None, strict=False, unscale=False, scaleParams=None, exponents=None, outputBounds=None, metadataOptions=None, outputSRS=None, GCPs=None, noData=None, rgbExpand=None, stats=False, rat=True, resampleAlg=None, callback=None, callback_data=None)
source code 
Create a TranslateOptions() object that can be passed to gdal.Translate()
Keyword arguments are :
  options --- can be be an array of strings, a string or let empty and filled from other keywords.
  format --- output format ("GTiff", etc...)
  outputType --- output type (gdal.GDT_Byte, etc...)
  bandList --- array of band numbers (index start at 1)
  maskBand --- mask band to generate or not ("none", "auto", "mask", 1, ...)
  width --- width of the output raster in pixel
  height --- height of the output raster in pixel
  widthPct --- width of the output raster in percentage (100 = original width)
  heightPct --- height of the output raster in percentage (100 = original height)
  xRes --- output horizontal resolution
  yRes --- output vertical resolution
  creationOptions --- list of creation options
  srcWin --- subwindow in pixels to extract: [left_x, top_y, width, height]
  projWin --- subwindow in projected coordinates to extract: [ulx, uly, lrx, lry]
  projWinSRS --- SRS in which projWin is expressed
  strict --- strict mode
  unscale --- unscale values with scale and offset metadata
  scaleParams --- list of scale parameters, each of the form [src_min,src_max] or [src_min,src_max,dst_min,dst_max]
  exponents --- list of exponentiation parameters
  outputBounds --- assigned output bounds: [ulx, uly, lrx, lry]
  metadataOptions --- list of metadata options
  outputSRS --- assigned output SRS
  GCPs --- list of GCPs
  noData --- nodata value (or "none" to unset it)
  rgbExpand --- Color palette expansion mode: "gray", "rgb", "rgba"
  stats --- whether to calculate statistics
  rat --- whether to write source RAT
  resampleAlg --- resampling mode
  callback --- callback method
  callback_data --- user data for callback

So, the same command showed above would be written in the following way:

from osgeo import gdal

kwargs = {
    'format': 'GTiff',
    'outputType': gdal.GDT_Int16
}

fn = 'input.tif'
dst_fn = 'output.tif'

ds = gdal.Translate(dst_fn, fn, **kwargs)
# do something with ds if you need
ds = None # close and save ds

Of course, you can pass more arguments if you need to.

1
  • you saved me again! I went with the subprocess.call option.and works great
    – Emtomp
    Commented Mar 2, 2020 at 16:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.