1

I'm trying to create an RGB GeoTIFF from an array. I've followed the question and response here, but I'm still having issues. When running gdalinfo on the image, the ColorInterp seem to be correct, but the image is grayscale when I open it in QGIS. Am I creating the file correctly, and/or is this an issue with viewing the image in QGIS?

The output image currently.

Here is my code, where the array is a 4-band image of size [4, 5000, 10000]. The bands are b, g, r, nir.

rows, cols = array[0].shape
bands = 4

driver = gdal.GetDriverByName("GTiff")
options = ["PHOTOMETRIC=RGB", "ALPHA=NO", "PROFILE=GeoTIFF"]

outdata = driver.Create(filename, cols, rows, bands, gdal.GDT_Float32, options=options)

outdata.SetGeoTransform(image.GetGeoTransform())
outdata.SetProjection(image.GetProjection())

for i in range(bands):
    outdata.GetRasterBand(i + 1).WriteArray(array[i, :, :])
    outdata.GetRasterBand(i + 1).SetNoDataValue(-999)

outdata.FlushCache()
outdata = None
array = None

What I expect to end up with is on the left, and what I do end up with is on the right:

enter image description here

3
  • Are you sure all of the bands in the array are different? In the QGIS properties > symbology, what's the renderer type?
    – mikewatt
    Commented Apr 9 at 0:32
  • @mikewatt The renderer type is multiband color, and contrast enhancement is set to "stretch to min/max." I've also selected a small AOI [5,5] to print out and make sure as I'm building the array, those sections of each band are not identical.
    – clrk
    Commented Apr 9 at 0:37
  • I can't reproduce. But your data isn't RGB, it's BGR (and N), so I wouldn't set PHOTOMETRIC=RGB. Additionally, QGIS seems to ignore the GDAL ColorInterp anyway (I'm running QGIS 3.36 on Linux) and sets RGB bands to 1,2,3 per the default raster rendering setting.
    – user2856
    Commented Apr 9 at 3:29

0

Your Answer

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

Browse other questions tagged or ask your own question.