Search code examples
pythonimagenumpygisgdal

Saving a large color image as `GTiff` with `gdal`


I'm trying to save a large image of size (15000, 80000, 3). This array is a numpy array that I initialized as im_final = np.zeros((15000,80000,,3)). To do the saving, I use gdal like so:

dst_ds = gdal.GetDriverByName('GTiff').Create('val.tif', 80000, 15000, 3, gdal.GDT_Byte)
dst_ds.GetRasterBand(1).WriteArray(im_final[:,:,0])   # write r-band to the    raster
dst_ds.GetRasterBand(2).WriteArray(im_final[:,:,1])   # write g-band to the raster
dst_ds.GetRasterBand(3).WriteArray(im_final[:,:,2])   # write b-band to the raster
dst_ds.FlushCache()                     # write to disk
dst_ds = None

When I save it, the resulting image is black and white. However, I need the image to be RGB, does anyone know what the issue is? Furthermore, the values in im_final are uint16.


Solution

  • The problem is you are trying to write uint16 into a uint8 (gdal.GDT_Byte) image. If you really need a 8-bit image (such as if you want to view this image in non-GIS programs), the best practice is to scale im_final to between 0-255. This could be a mapping from 0-65535 to 0-255 or min/max of each band to 0-255 or any other number of ways.

    If the values in im_final are important then use gdal.GDT_UInt16 in driver.Create().