[GIS] Unable to convert 3 band Spacenet dataset geotiff image to either jpg or png with gdal

gdal-translategeotiff-tiff

I have 3 band images beloging to the Spacenet dataset which I'm trying to convert to jpeg. I get the following warning:

Warning 6: JPEG driver doesn't support data type UInt16. Only eight bit byte bands supported.

I then changed the ouput to png, but no matter what I do, gdal_translate outputs completely black images (checked with Gimp, it's all 0s). The geographic information seems to be extracted correctly.

gdalinfo provides the following info:

Driver: GTiff/GeoTIFF
Files: RGB-PanSharpen_AOI_2_Vegas_img1.tif
Size is 650, 650
Coordinate System is:
GEOGCS["WGS 84",
    DATUM["WGS_1984",
        SPHEROID["WGS 84",6378137,298.257223563,
            AUTHORITY["EPSG","7030"]],
        AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0],
    UNIT["degree",0.0174532925199433],
    AUTHORITY["EPSG","4326"]]
Origin = (-115.307517599999997,36.128297699699999)
Pixel Size = (0.000002700000000,-0.000002700000000)
Metadata:
  AREA_OR_POINT=Area
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (-115.3075176,  36.1282977) (115d18'27.06"W, 36d 7'41.87"N)
Lower Left  (-115.3075176,  36.1265427) (115d18'27.06"W, 36d 7'35.55"N)
Upper Right (-115.3057626,  36.1282977) (115d18'20.75"W, 36d 7'41.87"N)
Lower Right (-115.3057626,  36.1265427) (115d18'20.75"W, 36d 7'35.55"N)
Center      (-115.3066401,  36.1274202) (115d18'23.90"W, 36d 7'38.71"N)
Band 1 Block=650x2 Type=UInt16, ColorInterp=Gray
Band 2 Block=650x2 Type=UInt16, ColorInterp=Undefined
Band 3 Block=650x2 Type=UInt16, ColorInterp=Undefined

I've also tried with mogrify, but it doen's work either. In fact, it seems to get stuck.

I'm not sure whether I'm missing an intermediate step of some kind or there's something wrong with my file.

The image is here.

Best Answer

Convert 0 to NoData:

gdal_translate -a_nodata 0 -of VRT RGB-PanSharpen_AOI_2_Vegas_img1.tif RGB-PanSharpen_AOI_2_Vegas_img1.vrt

Find image stats:

gdalinfo RGB-PanSharpen_AOI_2_Vegas_img1.vrt -stats

Driver: VRT/Virtual Raster
Files: RGB-PanSharpen_AOI_2_Vegas_img1.vrt
       /tmp/RGB-PanSharpen_AOI_2_Vegas_img1.tif
Size is 650, 650
<snip>
Band 1 Block=128x128 Type=UInt16, ColorInterp=Gray
  Min=30.000 Max=1525.000 
  Minimum=30.000, Maximum=1525.000, Mean=465.622, StdDev=221.362
  NoData Value=0
  Metadata:
    STATISTICS_MAXIMUM=1525
    STATISTICS_MEAN=465.62191430449
    STATISTICS_MINIMUM=30
    STATISTICS_STDDEV=221.36191405998
Band 2 Block=128x128 Type=UInt16, ColorInterp=Undefined
  Min=150.000 Max=1772.000 
  Minimum=150.000, Maximum=1772.000, Mean=597.099, StdDev=208.563
  NoData Value=0
  Metadata:
    STATISTICS_MAXIMUM=1772
    STATISTICS_MEAN=597.09910596959
    STATISTICS_MINIMUM=150
    STATISTICS_STDDEV=208.56265835534
Band 3 Block=128x128 Type=UInt16, ColorInterp=Undefined
  Min=171.000 Max=1202.000 
  Minimum=171.000, Maximum=1202.000, Mean=450.385, StdDev=118.278
  NoData Value=0
  Metadata:
    STATISTICS_MAXIMUM=1202
    STATISTICS_MEAN=450.38523089919
    STATISTICS_MINIMUM=171
    STATISTICS_STDDEV=118.27755953022

Use band min/max values to scale (or you could calculate a stddev scale or perhaps gdalinfo -hist etc... to calculate a percentage stretch)

gdal_translate -scale_1 30 1525 -scale_2 150 1772 -scale_3 171 1202  -ot Byte -of JPEG RGB-PanSharpen_AOI_2_Vegas_img1.vrt RGB-PanSharpen_AOI_2_Vegas_img1.jpg

PanSharpen_AOI_2_Vegas_img1.jpg

Or to output a white background:

gdalbuildvrt -srcnodata 0 -vrtnodata 65535 RGB-PanSharpen_AOI_2_Vegas_img1.vrt RGB-PanSharpen_AOI_2_Vegas_img1.tif
gdalinfo -stats etc...
gdal_translate etc...

PanSharpen_AOI_2_Vegas_img1.jpg

Related Question