0

I have been given some images in TIFF format, and I need to convert them to the PNG format. I have been given that the TIFF file contains 4 bands: R, G, B, and NIR (which I do not need for the PNG). Further, the TIFF file contains the pixels in uint16. On running gdalinfo -stats on a TIFF file, this is what I get:

Driver: GTiff/GeoTIFF
Files: 1.tif
   1.tif.aux.xml
Size is 1364, 1295
Coordinate System is:
PROJCS["WGS 84 / UTM zone 32N",
GEOGCS["WGS 84",
    DATUM["WGS_1984",
    SPHEROID["WGS 84",6378137,298.257223563,
        AUTHORITY["EPSG","7030"]],
    AUTHORITY["EPSG","6326"]],
    PRIMEM["Greenwich",0,
    AUTHORITY["EPSG","8901"]],
    UNIT["degree",0.0174532925199433,
    AUTHORITY["EPSG","9122"]],
    AUTHORITY["EPSG","4326"]],
PROJECTION["Transverse_Mercator"],
PARAMETER["latitude_of_origin",0],
PARAMETER["central_meridian",9],
PARAMETER["scale_factor",0.9996],
PARAMETER["false_easting",500000],
PARAMETER["false_northing",0],
UNIT["metre",1,
    AUTHORITY["EPSG","9001"]],
AXIS["Easting",EAST],
AXIS["Northing",NORTH],
AUTHORITY["EPSG","32632"]]
Origin = (471456.599999999685679,5252603.999999998137355)
Pixel Size = (0.600000000000145,-0.600000000000000)
Metadata:
  AREA_OR_POINT=Area
  Band_1=PC Sharpen (Band 1:02AUG16102514-M2AS-005695178010_01_P001.TIF)
  Band_2=PC Sharpen (Band 2:02AUG16102514-M2AS-005695178010_01_P001.TIF)
  Band_3=PC Sharpen (Band 3:02AUG16102514-M2AS-005695178010_01_P001.TIF)
  Band_4=PC Sharpen (Band 4:02AUG16102514-M2AS-005695178010_01_P001.TIF)
Image Structure Metadata:
  INTERLEAVE=PIXEL
Corner Coordinates:
Upper Left  (  471456.600, 5252604.000) (  8d37'17.52"E, 47d25'34.53"N)
Lower Left  (  471456.600, 5251827.000) (  8d37'17.70"E, 47d25' 9.37"N)
Upper Right (  472275.000, 5252604.000) (  8d37'56.59"E, 47d25'34.66"N)
Lower Right (  472275.000, 5251827.000) (  8d37'56.76"E, 47d25' 9.49"N)
Center      (  471865.800, 5252215.500) (  8d37'37.15"E, 47d25'22.01"N)
Band 1 Block=1364x1 Type=UInt16, ColorInterp=Gray
  Description = PC Sharpen (Band 1:02AUG16102514-M2AS-005695178010_01_P001.TIF)
  Min=10.000 Max=1118.000 
  Minimum=0.000, Maximum=1652.000, Mean=217.443, StdDev=89.146
  Metadata:
STATISTICS_MAXIMUM=1652
STATISTICS_MEAN=217.44318436577
STATISTICS_MINIMUM=0
STATISTICS_STDDEV=89.146238735783
Band 2 Block=1364x1 Type=UInt16, ColorInterp=Undefined
  Description = PC Sharpen (Band 2:02AUG16102514-M2AS-005695178010_01_P001.TIF)
  Min=30.000 Max=1705.000 
  Minimum=0.000, Maximum=2133.000, Mean=309.619, StdDev=145.897
  Metadata:
STATISTICS_MAXIMUM=2133
STATISTICS_MEAN=309.61859565892
STATISTICS_MINIMUM=0
STATISTICS_STDDEV=145.89744142331
Band 3 Block=1364x1 Type=UInt16, ColorInterp=Undefined
  Description = PC Sharpen (Band 3:02AUG16102514-M2AS-005695178010_01_P001.TIF)
  Min=0.000 Max=1594.000 
  Minimum=0.000, Maximum=2082.000, Mean=201.293, StdDev=126.679
  Metadata:
STATISTICS_MAXIMUM=2082
STATISTICS_MEAN=201.29347252573
STATISTICS_MINIMUM=0
STATISTICS_STDDEV=126.67918628134
Band 4 Block=1364x1 Type=UInt16, ColorInterp=Undefined
  Description = PC Sharpen (Band 4:02AUG16102514-M2AS-005695178010_01_P001.TIF)
  Minimum=0.000, Maximum=3102.000, Mean=453.964, StdDev=208.556
  Metadata:
STATISTICS_MAXIMUM=3102
STATISTICS_MEAN=453.96443347411
STATISTICS_MINIMUM=0
STATISTICS_STDDEV=208.55563519719

When I convert it via gdal_translate, I get a black image without the -scale option, as the maximum values are far from the uint16 maximum (65535). With the -scale option (and after removing the alpha channel using ImageMagick), this is what I get: enter image description here

However, this is not the actual output, as what I can view in QGIS is this (image obtained by exporting the map; which does not keep the original tif image size): enter image description here

How is it that QGIS gets the correct output? I have tried obtaining the numpy array format of the tif file and manipulating it to get the output to match what I get in QGIS in vain. Since I need to convert multiple TIFF files to PNG, I need to write a script to process all of the TIFF files. How do I do this?

Your Answer

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

Browse other questions tagged or ask your own question.