Skip to content

Instantly share code, notes, and snippets.

@donfleming
Last active August 7, 2017 15:11
Show Gist options
  • Save donfleming/3ae0c63463ebb9326ef1 to your computer and use it in GitHub Desktop.
Save donfleming/3ae0c63463ebb9326ef1 to your computer and use it in GitHub Desktop.
import arcpy, os
from arcpy import env
from PIL import Image
# Move the shapefiles to a file geodatabase
# Set the workspace where we will find the shapefiles
env.workspace = "/target workspace containing the shapefiles/"
env.overwriteOutput = True
targetGDB = "/path to your file geodatabase/"
# Create a list object of the feature classes in the target folder
fcList = arcpy.ListFeatureClasses()
# Copy the shapefiles to the file geodatabase
arcpy.FeatureClassToGeodatabase_conversion(fcList, targetGDB)
# Change the workspace to the target file geodatabase
env.workspace = targetGDB
# Create a new list of feature class to iterate through
fcList = arcpy.ListFeatureClasses()
# Declare the map document object
mxd = arcpy.mapping.MapDocument("/path to your .mxd/")
# Create a data frame object from the single data frame in our mxd
data_frame = arcpy.mapping.ListDataFrames(mxd)[0]
# Set the active view for our mxd to the data frame - reference it using its name
mxd.activeView = data_frame.name
# Target directory for the PNGs
outDir = "/path to target directory/"
# Iterate through each of the feature classes in our list
for fc in fcList:
# Store the name of the feature class in a variable
lyrName = arcpy.Describe(fc).name
# Convert the feature class to a layer
arcpy.MakeFeatureLayer_management(fc, lyrName)
lyr = arcpy.mapping.Layer(lyrName)
# Add the layer to the data frame
arcpy.mapping.AddLayer(data_frame, lyr, "TOP")
# These are just to make sure our layer is visible and the data frame is refreshed
lyr.visible = True
arcpy.RefreshTOC()
arcpy.RefreshActiveView()
# select the layer in the data frame
arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION")
# Get the extent of the layer
lyrExtent = lyr.getSelectedExtent()
# Change the extent of the data frame to the extent of our layer
data_frame.extent = lyrExtent
# Export to PNG
arcpy.mapping.ExportToPNG(mxd, outDir + str(arcpy.Describe(lyr).nameString) + ".png", data_frame, df_export_width = 10000, df_export_height = 10000)
# Make sure the data frame is empty before ending the loop.
for i in arcpy.mapping.ListLayers(mxd,"", data_frame):
arcpy.mapping.RemoveLayer(data_frame, i)
# Cleaning up after ourselves. Delete the layer before moving on to the next feature class
del lyr
# Turning our maps black
# Set the path to the directory containing PNG files.
path = "/path to directory containing output PNGs/"
fileList = os.listdir(path)
# Loop through all of the PNGs
for img in fileList:
# Open the image
im = Image.open(path + img)
# Load the image for use
pix = im.load()
# Assign the height and width to variables
w,h = im.size
# Assign RGB values for black and white to tuples
black = (0,0,0)
white = (255,255,255)
# Loop through all of the pixels
for x in range(w):
for y in range(h):
# Check if pixel RGB value is not black and is not white. If not, change its RGB to black
if pix[x,y] != black and pix[x,y] != white:
pix[x,y] = black
# Save the PNG to target directory, removing "_rds" from the filename.
im.save("/path to target directory for final PNGs/" + img.replace("_rds",""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment