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
os.chdir(path)
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')
driver.Register()
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.FlushCache()
dsband = None
ds = None.