<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Normalize raster bands and recombine in a loop? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/normalize-raster-bands-and-recombine-in-a-loop/m-p/175466#M13486</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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:&lt;BR /&gt;&lt;BR /&gt;folder = &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"C:/myfolder/subfolder"&lt;BR /&gt;&lt;/SPAN&gt;env.workspace = folder&lt;BR /&gt;rasterlist = arcpy.ListRasters()&lt;BR /&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;raster &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;rasterlist:&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;env.workspace = raster&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;bands = [Raster(os.path.join(raster, b)) &lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;b &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;arcpy.ListRasters()]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;env.workspace = folder&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;normexgreen = ((bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;] / (bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;])) * &lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;) - (&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;(bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] / (bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;])) - (bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;] / (bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;])))&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;normexgreen.save(os.path.splitext(raster)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"_NXG" &lt;/SPAN&gt;+ &lt;SPAN style="color: #008080; font-weight: bold;"&gt;".tif"&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am now adding to the complexity by normalizing the bands of each RGB raster first by way of the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;norm_red = ((bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)/((bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)))&lt;BR /&gt;norm_green = ((bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)/((bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)))&lt;BR /&gt;norm_blue= ((bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)/((bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;norm_red.save(os.path.splitext(raster)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"norm_red" &lt;/SPAN&gt;+ &lt;SPAN style="color: #008080; font-weight: bold;"&gt;".tif"&lt;/SPAN&gt;)&lt;BR /&gt;norm_green.save(os.path.splitext(raster)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"norm_green" &lt;/SPAN&gt;+ &lt;SPAN style="color: #008080; font-weight: bold;"&gt;".tif"&lt;/SPAN&gt;)&lt;BR /&gt;norm_blue.save(os.path.splitext(raster)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"norm_blue" &lt;/SPAN&gt;+ &lt;SPAN style="color: #008080; font-weight: bold;"&gt;".tif"&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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 &lt;BR /&gt;could then go:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;norm_RGB = arcpy.CompositeBands_management("norm_red;norm_green;norm_blue", "normalizedRGB.tif")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I have MULTIPLE rasters that were ALL separated into three individual bands and need to be recombined correctly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What can I do?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 06 Dec 2019 23:35:38 GMT</pubDate>
    <dc:creator>AnneFarineau1</dc:creator>
    <dc:date>2019-12-06T23:35:38Z</dc:date>
    <item>
      <title>Normalize raster bands and recombine in a loop?</title>
      <link>https://community.esri.com/t5/python-questions/normalize-raster-bands-and-recombine-in-a-loop/m-p/175466#M13486</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;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:&lt;BR /&gt;&lt;BR /&gt;folder = &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"C:/myfolder/subfolder"&lt;BR /&gt;&lt;/SPAN&gt;env.workspace = folder&lt;BR /&gt;rasterlist = arcpy.ListRasters()&lt;BR /&gt;&lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;raster &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;rasterlist:&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;env.workspace = raster&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;bands = [Raster(os.path.join(raster, b)) &lt;SPAN style="color: #000080; font-weight: bold;"&gt;for &lt;/SPAN&gt;b &lt;SPAN style="color: #000080; font-weight: bold;"&gt;in &lt;/SPAN&gt;arcpy.ListRasters()]&lt;BR /&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;env.workspace = folder&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;normexgreen = ((bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;] / (bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;])) * &lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;) - (&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;(bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] / (bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;])) - (bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;] / (bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;] + bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;])))&lt;BR /&gt; &amp;nbsp;&amp;nbsp;&amp;nbsp;normexgreen.save(os.path.splitext(raster)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"_NXG" &lt;/SPAN&gt;+ &lt;SPAN style="color: #008080; font-weight: bold;"&gt;".tif"&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am now adding to the complexity by normalizing the bands of each RGB raster first by way of the following:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;norm_red = ((bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)/((bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)))&lt;BR /&gt;norm_green = ((bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)/((bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)))&lt;BR /&gt;norm_blue= ((bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)/((bands[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;1&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)+(bands[&lt;SPAN style="color: #0000ff;"&gt;2&lt;/SPAN&gt;]/&lt;SPAN style="color: #0000ff;"&gt;255&lt;/SPAN&gt;)))&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;norm_red.save(os.path.splitext(raster)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"norm_red" &lt;/SPAN&gt;+ &lt;SPAN style="color: #008080; font-weight: bold;"&gt;".tif"&lt;/SPAN&gt;)&lt;BR /&gt;norm_green.save(os.path.splitext(raster)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"norm_green" &lt;/SPAN&gt;+ &lt;SPAN style="color: #008080; font-weight: bold;"&gt;".tif"&lt;/SPAN&gt;)&lt;BR /&gt;norm_blue.save(os.path.splitext(raster)[&lt;SPAN style="color: #0000ff;"&gt;0&lt;/SPAN&gt;] + &lt;SPAN style="color: #008080; font-weight: bold;"&gt;"norm_blue" &lt;/SPAN&gt;+ &lt;SPAN style="color: #008080; font-weight: bold;"&gt;".tif"&lt;/SPAN&gt;)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;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 &lt;BR /&gt;could then go:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;norm_RGB = arcpy.CompositeBands_management("norm_red;norm_green;norm_blue", "normalizedRGB.tif")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I have MULTIPLE rasters that were ALL separated into three individual bands and need to be recombined correctly.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What can I do?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 06 Dec 2019 23:35:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/normalize-raster-bands-and-recombine-in-a-loop/m-p/175466#M13486</guid>
      <dc:creator>AnneFarineau1</dc:creator>
      <dc:date>2019-12-06T23:35:38Z</dc:date>
    </item>
  </channel>
</rss>

