I am trying to use the SRTM earth images to convert them into heightmaps for 3D application.

I can output a 8-bit PNG like this:

gdal_translate -ot Byte -scale -of PNG srtm_44_05.tif heightmap.png

Opening Photoshop/Paint.net confirms it's legit. However I want the extra precision of 16-bits...

If I try to output a 16-bit PNG however like this:

gdal_translate -ot Uint16 -scale -of PNG srtm_44_05.tif heightmap.png

It's just a black image when viewed in above applications. I am suspecting it has something to do with the byte ordering being wrong...?

For example if I output a RAW image like this

gdal_translate -ot Uint16 -scale -of ENVI srtm_44_05.tif heightmap.raw

And then open it in Photoshop and have it interpret it as 16-bit with one channel in MAC byte ordering then it looks fine. If I specify PC byte ordering it's just like the PNG (black image).

I have found a tedious workaround by using gal_translate to produce 16-bit RAW and then open it with Photoshop and export it as 16-bit PNG. That works but it shouldnt be necessary.

Is the byte ordering the problem? What can I do to produce legit 16-bit PNG images?


You are using the -scale option of gdal_translate without other parameters and that's why your 16 bit png appears black.

From https://gdal.org/programs/gdal_translate.html

-scale [src_min src_max [dst_min dst_max]] Rescale the input pixels values from the range src_min to src_max to the range dst_min to dst_max. If omitted the output range is 0 to 255. If omitted the input range is automatically computed from the source data.

Run gdalinfo -stats heightmap.png for your 16 bit png and you'll see that the maximum value is something close to 255. It may be somewhat lower or higher because GDAL does not cut the scale at given min/max values. They are just values that it is using for computing the linear interpolation.

There is no automatic scaling option for other datatypes than 8 bit. You must check the min/max values of the source image with gdalinfo and then write the ranges manually. For a random SRTM file I downloaded the parameters to use were

gdal_translate -ot Uint16 -scale 41 2424 0 32767 -of PNG srtm_40_03.tif 16bitscale.png

Image looks dark because there are not so much mountain tops in this view.

enter image description here

Be aware that with this command the older GDAL versions are just copying the nodata value from the original image and in this case when output is unsigned 16 bit that does not make sense:

NoData Value=-32768

At least 3.1dev version is changing the nodata value into 0 that may also be wrong for you because as scaled the 16 bit png will contain real data with value 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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