import arcpy debugmsg = "" # Receive the parameters from the toolbox dialog # - the path of the input image in_img_path = arcpy.GetParameterAsText(0) debugmsg += "input: " + in_img_path + "\n" # - the choice of outputting a debug text file debug = arcpy.GetParameter(1) # set up variables tmp = in_img_path.split("\\") layername = tmp[len(tmp)-1].split(".")[0] debugmsg += "layer name: " + layername + "\n" try: # -- generate a raster layer from image arcpy.MakeRasterLayer_management(imgname, layername, "#") debugmsg += arcpy.GetMessages()+"\n" except: debugmsg += "Make Raster Layer failed.\n" debugmsg += arcpy.GetMessages()+"\n" print debugmsg if debug: f = open(in_img_path[0:len(in_img_path)-4]+'_debug.txt', 'w') f.write(debugmsg) f.close()
input: I:\WIN\Desktop\image\big.png layer name: big Make Raster Layer failed.
arcpy.MakeRasterLayer_management(r"I:\WIN\Desktop\image\big.png","big")
Solved! Go to Solution.
mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] lyr = arcpy.mapping.Layer(layername) arcpy.mapping.AddLayer(df, lyr) arcpy.RefreshTOC() arcpy.RefreshActiveView()
arcpy.MakeRasterLayer_management(in_img_path, layername, "#")
Hello,
In your MakeRasterLayer function, it doesn't look like you have the 'imgname' variable defined to anything. Try the following instead:arcpy.MakeRasterLayer_management(in_img_path, layername, "#")
mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] lyr = arcpy.mapping.Layer(layername) arcpy.mapping.AddLayer(df, lyr) arcpy.RefreshTOC() arcpy.RefreshActiveView()
Add the following to the end of your code:mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd)[0] lyr = arcpy.mapping.Layer(layername) arcpy.mapping.AddLayer(df, lyr) arcpy.RefreshTOC() arcpy.RefreshActiveView()