1

I am trying to convert a single band GeoTIFF file to PNG with pseudocolor. What I am trying to achieve is not a thumbnail but an image with geospatial reference that I can serve as an Image Overlay in Leaflet or OpenLayers in the future.

I am using the rasterio library.

import rasterio

with rasterio.Env():
    with rasterio.open('example.tif') as src:
        Z = src.read(1)
        meta = src.meta
        meta['driver'] = 'PNG'
        meta['dtype'] = 'uint16'
        meta['nodata'] = 32768.0
        print(meta)
        
    with rasterio.open('colormap.png', 'w', **meta) as dst:
        dst.write(Z, 1)
        dst.write_colormap(
            1, {
                1637: (255, 0, 0, 255),
                5191: (0, 0, 255, 255) })

The values 1637 and 5191 are the converted minimum and maximum uint16 values from the .tif file. When running this script: I get the error:

CPLE_AppDefinedError: libpng: Invalid IHDR data

The example image can be found HERE.

Other libraries such as gdal or opencv as alternative is also welcome.

1
  • 1
    I haven't addressed this in my answer below as it's not relevant to the issue, but note your example.tif has a nodata value of -32768 (negative). Even though you specify that the nodata value is 32768 (positive) when opening your output for writing, you aren't actually changing the -32768 values in the data/array to 32768...
    – user2856
    Jan 30, 2023 at 4:41

1 Answer 1

1

The PNG format doesn't support 16bit indexed (with colormap):

Pixel formats

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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