ArcPy – Fix Unsupported Operands Type Error in For Cycle

arcpyerror

I'm trying to do a for cycle to calculate standard deviation and mean for each raster in a list and after that to do a mathematical operation between them. Can someone help me to understand what is the problem?

ws2 = r"C:\Users\Elisa\Model_Builder\passo_5_6_7_8\output\raster_tagliati_su_flowacc"
v_name_sc_img = r"C:\Users\Elisa\Model_Builder\passo_5_6_7_8\output\outliers\{0}_out1.img"
arcpy.env.workspace = ws2
ras_names = arcpy.ListRasters()
for ras_name in ras_names:
    name, ext = os.path.splitext(ras_name)
    ras = arcpy.Raster(os.path.join(ws2, ras_name))
    mean =arcpy.GetRasterProperties_management(ras, "MEAN")
    STD=arcpy.GetRasterProperties_management(ras, "STD")
    out_ras = ras - (mean +  3 * STD )
    out_ras.save(v_name_sc_img.format(name))

Best Answer

GetRasterProperties is returning a Result object, not the actual value of for example mean:

The Python result of this tool is a geoprocessing Result object. In order to obtain the string value, use the Result object's getOutput method.

Try using getOutput method. The output will be a string so convert it to float (or int if you want):

mean =float(arcpy.GetRasterProperties_management(ras, "MEAN").getOutput(0).replace(',','.'))
STD=float(arcpy.GetRasterProperties_management(ras, "STD").getOutput(0).replace(',','.'))