0

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.

  • React to warning message and try converting to byte by adding "-ot byte" to your command if you need jpeg. – user30184 Jun 15 '17 at 11:11
  • What's wrong with leaving it as a GTiff? – MappaGnosis Jun 15 '17 at 11:11
  • I do need jpgs to train a CNN. Added -ot byte and got rid of warning, but the output still wrong – jsa Jun 15 '17 at 11:45
  • Update: With gdal_translate -of JPEG -ot byte -b 1 AA.tif ccc.png. I get a grayscale, that looks like an over exposed picture. Maybe it's a matter of telling gdal how to interpret the other layers, maybe it's an RGB image and gdal it's getting it wrong. – jsa Jun 15 '17 at 12:04
  • It is also important to study the histogram and scale if needed as in the answer by @Luke. Maximum value of UInt16 is 65535 but the biggest value in that image is only 1772. That is 2.7% of the whole scale. Why do you use -b 1 if you do not want to get a grayscale image? – user30184 Jun 15 '17 at 13:18
2

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

  • I'm afraid gdal should be in my list of things to learn in the near future. Thank you! – jsa Jun 16 '17 at 7:07
  • For single-band imagery, you can use only generic "-scale" and skip the stats and "-scale_1 {min} {max}" gdal_translate parameter – user3076252 Jul 13 '17 at 19:41

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.