0

When using gdal_translate on the command-line, there is an option to set a scale factor without modifying pixel values using the a_scale option (see here).

I want to use this option in Python, e.g. like this:

ds = gdal.Translate(fp_out, correctedImage, a_scale = 0.01)

But I don't see the options listed in the docstring of gdal.TranslateOptions and so the above doesn't work:

  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 (gdalconst.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
  nogcp --- ignore GCP in the raster
  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
  xmp --- whether to copy XMP metadata
  resampleAlg --- resampling mode
  callback --- callback method
  callback_data --- user data for callback

Any ideas on how to get this option to work?

0

1 Answer 1

1

Maybe a workaround could be to read the source code of gdal_edit.py https://gdal.org/programs/gdal_edit.html from https://github.com/OSGeo/gdal/blob/master/swig/python/gdal-utils/osgeo_utils/gdal_edit.py and edit the scale in the same way with your own script

 if scale:
        for i in range(ds.RasterCount):
            ds.GetRasterBand(i + 1).SetScale(scale[i])

    if offset:
        for i in range(ds.RasterCount):
            ds.GetRasterBand(i + 1).SetOffset(offset[i])

But by looking at the C++ source code of gdal_translate_lib https://github.com/OSGeo/gdal/blob/master/apps/gdal_translate_lib.cpp#L2789 it looks like the option should be "-a_scale" instead of "a_scale" that you used.

1
  • Thanks, you are right. I can indeed just pass a gdal.Dataset (instead of a path) to gdal.Translate and add the scale factors to the bands beforehand like you suggest. Commented Sep 19, 2022 at 10:26

Your Answer

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

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