I split a 3-band raster image up into 100 x 100 pixel tiles in QGIS. I need each tile to render the same colour scale as the bigger, original raster image it was produced from. I know you can render it properly with a virtual tile set, but the information needs to be permanently changed for use outside of GIS.

I adapted a script to force the original min-max values for each band, which appears to work when I check the tile headers, however, when I load up the tiles they still render at their own individual colour scales. How do I permanently fix this?

import os 
import gdal 

path = <"dir/of/tiles"> #location of all tiles

tiles = [f for f in os.listdir(path) if f.endswith('.tif')]

bmin = 114 #min blue band value of original (non tile) raster
bmax = 163 #max blue band value of original (non tile) raster
gmin = 91
gmax = 176
rmin = 57
rmax = 168

for i in tiles:

    driver = gdal.GetDriverByName('GTiff')
    ds = gdal.Open(tiles[i], gdal.GA_Update)
    dsband = ds.GetRasterBand(1)
    (newmin, newmax)= dsband.ComputeRasterMinMax(0)
    (newmean, newstdv) = dsband.ComputeBandStats(1)
    dsband.SetStatistics(bmin, bmax,newmean, newstdv)#overwriting the tile min
    dsband = ds.GetRasterBand(2)
    (newmin, newmax)= dsband.ComputeRasterMinMax(0)
    (newmean, newstdv) = dsband.ComputeBandStats(1)
    dsband.SetStatistics(gmin, gmax,newmean, newstdv)
    dsband = ds.GetRasterBand(3)
    (newmin, newmax)= dsband.ComputeRasterMinMax(0)
    (newmean, newstdv) = dsband.ComputeBandStats(1)
    dsband.SetStatistics(rmin, rmax,newmean, newstdv)
    dsband = None  
    ds = None.
  • Would be very helpful if you could share some sample data somehow. Also, how do you mean "split" the file into tiles? It appears you have done this in a pre-processing step outside the script you shared, but it's not clear
    – 15Step
    Dec 31, 2021 at 14:21


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.

Browse other questions tagged or ask your own question.