Normalize raster bands and recombine in a loop?

12-06-2019 03:35 PM
New Contributor

I have managed to glean enough bits and pieces from other people's work to put together a loop that pulls apart the individual bands of a raster and recombines them in a meaningful way, then saves each according to the original raster file name. For example:

folder = "C:/myfolder/subfolder"
env.workspace = folder
rasterlist = arcpy.ListRasters()
for raster in rasterlist:
   env.workspace = raster
   bands = [Raster(os.path.join(raster, b)) for b in arcpy.ListRasters()]
   env.workspace = folder
   normexgreen = ((bands[1] / (bands[0] + bands[1] + bands[2])) * 2) - (
   (bands[0] / (bands[0] + bands[1] + bands[2])) - (bands[2] / (bands[0] + bands[1] + bands[2])))[0] + "_NXG" + ".tif")

I am now adding to the complexity by normalizing the bands of each RGB raster first by way of the following:

norm_red = ((bands[0]/255)/((bands[0]/255)+(bands[1]/255)+(bands[2]/255)))
norm_green = ((bands[1]/255)/((bands[0]/255)+(bands[1]/255)+(bands[2]/255)))
norm_blue= ((bands[2]/255)/((bands[0]/255)+(bands[1]/255)+(bands[2]/255)))

However, I cannot recombine them when they only exist in temporary space, so I must save them as permanent files before I can recombine them:[0] + "norm_red" + ".tif")[0] + "norm_green" + ".tif")[0] + "norm_blue" + ".tif")

In this way I save them with the name of the original 3-band raster file that it came from. If this was just one raster I
could then go:

norm_RGB = arcpy.CompositeBands_management("norm_red;norm_green;norm_blue", "normalizedRGB.tif")

But I have MULTIPLE rasters that were ALL separated into three individual bands and need to be recombined correctly.

What can I do?

0 Kudos
0 Replies