0

I need to use rasterio.open() to read a color PNG image, but sometimes the file only returns 1 band. Why does this happen? Is there anything I can do to fix it?

import rasterio

# I can got 4 bands
url_png = r'https://estatico-redemet.decea.mil.br/satelite/2023/05/08/realcada/maps/realcada_202305081140.png'

data_img = rasterio.open(url_png,'r').read([1, 2, 3, 4])

# I can't got 4 bands

url_png = r'https://estatico-redemet.decea.mil.br/satelite/2023/05/08/realcada/maps/realcada_202305080740.png'

data_img = rasterio.open(url_png,'r').read([1, 2, 3, 4])

1 Answer 1

1

Rasterio finds only 1 band because the second png file is a single band image with a color palette. Test with gdalinfo shows

gdalinfo downloaded.png
Driver: PNG/Portable Network Graphics
Files: downloaded.png
Size is 856, 783
Corner Coordinates:
...
Band 1 Block=856x1 Type=Byte, ColorInterp=Palette
  Color Table (RGB with 256 entries)
0: 0,0,0,9
1: 0,0,0,5
2: 20,20,20,12
3: 16,16,16,16
4: 20,20,20,24
5: 17,17,17,14
...

The first file has four bands

Blockquote

Band 1 Block=856x1 Type=Byte, ColorInterp=Red
  Mask Flags: PER_DATASET ALPHA
Band 2 Block=856x1 Type=Byte, ColorInterp=Green
  Mask Flags: PER_DATASET ALPHA
Band 3 Block=856x1 Type=Byte, ColorInterp=Blue
  Mask Flags: PER_DATASET ALPHA
Band 4 Block=856x1 Type=Byte, ColorInterp=Alpha

There is not really anything to fix in the paletted image because it is not wrong. However, if you need RGB for further processing it is possible to expand the paletted image into RGB for example with https://gdal.org/programs/pct2rgb.html.

This utility will convert a pseudo-color band on the input file into an output RGB file of the desired format.

Your Answer

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

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