1

My question relates to the answer found at Exporting data frame as PNG in ModelBuilder using ArcMap

I've followed the steps in the answer provided by @PolyGeo, but my output PNG is just a blank image.

I also added a few extra lines of code from ExportToPNG example 2 here to control the size of the PNG, because by default it's 640 by 480 pixels:

https://desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-mapping/exporttopng.htm

So my final code is:


import arcpy

lyrFile = arcpy.GetParameterAsText(0)
pngFile = arcpy.GetParameterAsText(1)

mxd = arcpy.mapping.MapDocument("D:/Old C Drive/School/Base Data/Scripts/Untitled.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
addLayer = arcpy.mapping.Layer(lyrFile)
arcpy.mapping.ExportToPNG(mxd, pngFile, df,
    df_export_width=1920,
    df_export_height=1080)
del mxd

My mxd is totally empty, so I'm not sure where I went wrong. I did see in the original question someone mentioned that using a blank document didn't work for them either so they used another mxd and it worked. I tried that but no luck.

1 Answer 1

2

The code you used was originally from me, and I left out a line. I just tested the code below successfully using ArcMap 10.8.

import arcpy

lyrFile = r"C:\temp\testPolys.lyr"          
pngFile = r"C:\temp\testPolys.png"

mxd = arcpy.mapping.MapDocument(r"C:\temp\test.mxd")
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
addLayer = arcpy.mapping.Layer(lyrFile)
arcpy.mapping.AddLayer(df, addLayer)
arcpy.mapping.ExportToPNG(mxd, pngFile, df)
del mxd

The line I had forgotton to include was:

arcpy.mapping.AddLayer(df, addLayer)

I had made a Layer object (named addLayer) from the layer file, but forgot to add that Layer object as a new layer in my map. I have now corrected my answer on the original question accordingly.

2
  • It worked! Thank you for the quick response! I just have one question, is there a reason for the blank mxd? I assume you send the layer file to it so that you have a clean slate to work with, is that correct? Commented Mar 24, 2021 at 1:34
  • 1
    Yes - that’s the main reason but I think it also means that the extent gets set by the layer added and not left at wherever the data frame is set. Commented Mar 24, 2021 at 2:24

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.