Skip to content

Instantly share code, notes, and snippets.

@mdsumner
Last active September 8, 2023 02:25
Show Gist options
  • Save mdsumner/ee4103d8616b9aa341e82c46b44a8c8c to your computer and use it in GitHub Desktop.
Save mdsumner/ee4103d8616b9aa341e82c46b44a8c8c to your computer and use it in GitHub Desktop.

gdal_translate -scale

from osgeo import gdal
ds = gdal.Translate("/vsimem/scl.tif", "autotest/gcore/data/float32.tif", options = "-scale")
ds.GetRasterBand(1).ComputeRasterMinMax()
#(0.0, 255.99899291992188)

replace "autotest/..." above with this for general online use without the source tree

ds = gdal.Translate("/vsimem/scl.tif", "/vsicurl/https://github.com/OSGeo/gdal/raw/master/autotest/gcore/data/float24.tif", options = "-scale")

generate specific data:

from osgeo import gdal
import struct

ln = 16
ds = gdal.GetDriverByName("GTiff").Create("/vsimem/scaled.tif", 1, ln, 1, gdal.GDT_Float32)
ds.GetRasterBand(1).WriteRaster(0, 0, 1, ln, b"".join(struct.pack("f", v) for v in range(ln)))
ds = None

ds = gdal.Translate("/vsimem/scaled1.tif", "/vsimem/scaled.tif", options = "-scale")
ds.GetRasterBand(1).ComputeRasterMinMax()
# (0.0, 255.99899291992188)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment