I am working with rasters in arcpy and I am not able to access individual bands using the Raster class. The same code that worked about a year ago no longer works.
I should be able to access a single band of a raster like so:
Band3 = Raster(raster_filename + "/Band_3")
But the resulting Band3 variable has all 6 bands in the raster, not just band 3.
The "/Band_3"
syntax appears to work fine in the Describe method in the first half of the following code but doesn't work in the Raster class later on:
folder = r"C:\Users\elyons\Desktop\Spring_2018\Labs\New_LabData"
import arcpy
from arcpy.sa import *
arcpy.CheckOutExtension("Spatial")
arcpy.env.workspace = folder
arcpy.env.overwriteOutput = True
raster_filename = "Landsat.tif"
#Create describe objects
desc = arcpy.Describe(raster_filename)
desc_band = arcpy.Describe(raster_filename + "/Band_3")
#These are properties of the describe raster BAND object
print "Height = " + str(desc_band.height)
print "Width = " + str(desc_band.width)
print "Resolution = " + str(desc_band.meanCellWidth)
#This one is a property of the describe raster dataset object
print "Number of Bands is = " + str(desc.bandCount)
#Open bands as raster band objects
Band3 = Raster(raster_filename + "/Band_3")
#print number of bands
print Band3.bandCount
#Save the raster
print "saving"
Band3.save("savebandtest.tif")
print "saved"
arcpy.CheckOutExtension("Spatial")
The above code prints out the correct information for the describe objects and then prints 6
for Band3.bandCount
, which is not right. It then saves a six band raster to the filename savebandtest.tif
that is identical to Landsat.tif
. I have tried different multiband rasters, different computers, getting the band name programatically, setting the working directory to the raster, forward slashes, backward slashes, everything I can think of.
Has the syntax changed for accessing single bands from multiband raster files? I can't find any mention of a change in the help.