1

I am kind of new to python but I am trying to batch process single band raster images to create multispectral imageries using arcpy Composite Bands. I have 24 single band rasters (Bands 1 - 3 ) all saved in a single folder which would create 8 composite images after its done. Fortunately the bands are in sequential order for each set, so I can input every three images without having to order them. So far I have:

arcpy.env.workspace = "C:\\Users\\Vinny\\Desktop\\Sentinel2_Scripts\\Unzipped\\Test_Out"

   direc = r"C:\\Users\\Vinny\\Desktop\\Sentinel2_Scripts\\Unzipped\\Test_Out"
out_raster = "C:\\Users\\Vinny\\Desktop\\Sentinel2_Scripts\\Unzipped\\Test_Out\\Composite_Bands"

i = 0
rasters = arcpy.ListRasters("*", "JP2")
for raster in rasters:
    i+=1
    arcpy.CompositeBands_management(raster, out_raster + "\\" + out)

Can you offer any suggestions or ideas?

  • 1
    What happens when you run your code? Where precisely are you stuck? – PolyGeo Mar 2 '17 at 20:12
  • After I've ran the code, it created 24 images . I am not too sure which bands or how many bands it was used to create those images but I need to limit three bands per run and have the loop recognize every three down the list of files. – Vince Mar 2 '17 at 20:29
  • Please use the edit button beneath your question to revise it with those additional details to try and focus what it is that you are asking. – PolyGeo Mar 2 '17 at 20:32
1

Your problem is on your iteration.. the composite bands command is run for each image. Try it this way:

import arcpy,os

arcpy.env.workspace = "C:\\Users\\Vinny\\Desktop\\Sentinel2_Scripts\\Unzipped\\Test_Out"
direc = r"C:\\Users\\Vinny\\Desktop\\Sentinel2_Scripts\\Unzipped\\Test_Out"
out_raster = "C:\\Users\\Vinny\\Desktop\\Sentinel2_Scripts\\Unzipped\\Test_Out\\Composite_Bands"

rasters = arcpy.ListRasters("*", "JP2")
rList = []
for raster in rasters:
    rList.append(os.path.join(direc,raster))
    if len(rList) == 3:
      arcpy.CompositeBands_management(rList, out_raster + "\\" + out) #what is the value of out?
      rList = [] # a new empty list

This holds back the running of CompositeBands until 3 images are present in the list and then resets the list back to empty. Note that nowhere in your code is the value of out set, I assume it's on a line that's been removed.

The example for CompositeBands has a semicolon separated string but the doc indicates that a python list is acceptable for the inputs. If the input is truly a semicolon separated list you can use string.join(rList,';') to convert the list to a string with delimiters (don't forget to import string first).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.