Select to view content in your preferred language

How to add an image to an existing data frame using Python?

4094
6
Jump to solution
01-09-2013 11:53 AM
MeganMiller4
Deactivated User
This is the first time I am using Python on my own, and I have searched for this answer for a few hours with no luck. I am wanting to take a .JPG image and put it in an existing data frame. The image has to be in color, and needs to change with data driven pages.  Help would be greatly appreciated. Thanks!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor
You can specify the specific data frame you want to add the raster to by using it's name in the following line:

df = arcpy.mapping.ListDataFrames(mxd, "<data frame name>")[0]


To use the names in the list, you could do something like:

for name in list:     arcpy.MakeRasterLayer_management(r"C:\ArcGISProjects\SamplePhotos" + "\\" + name, name)

View solution in original post

0 Kudos
6 Replies
JakeSkinner
Esri Esteemed Contributor
Hi Megan,

First, make a Raster Layer from the JPEG, and then use the AddLayer function to add the raster layer to the MXD.  Ex:

import arcpy

arcpy.MakeRasterLayer_management(r"C:\DATA\RASTER\JPG\barberspt.jpg", "BarbersPt")

mxd = arcpy.mapping.MapDocument(r"C:\Temp\Python\Harbour.mxd")
df = arcpy.mapping.ListDataFrames(mxd)[0]
addLayer = arcpy.mapping.Layer("BarbersPt")
arcpy.mapping.AddLayer(df, addLayer, "BOTTOM")
mxd.save()

del mxd
0 Kudos
MeganMiller4
Deactivated User
This helped with inserting the photo in color.  It kept putting the photo on the top most data frame, but I moved my data frame that needed the photo to the top and that is fixed.

I have ran a code to generate a list of names, but is there away to take the list generated from this code, and insert it here?

arcpy.MakeRasterLayer_management(r"C:\ArcGISProjects\SamplePhotos\BoraBoraTurtles.jpg", "BoraBoraTurtles")
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You can specify the specific data frame you want to add the raster to by using it's name in the following line:

df = arcpy.mapping.ListDataFrames(mxd, "<data frame name>")[0]


To use the names in the list, you could do something like:

for name in list:     arcpy.MakeRasterLayer_management(r"C:\ArcGISProjects\SamplePhotos" + "\\" + name, name)
0 Kudos
MeganMiller4
Deactivated User
I am really new to this coding thing, but my files are name.jpg.  How do I get the .jpg to attach to the picture name after name?
0 Kudos
JakeSkinner
Esri Esteemed Contributor
You would just need to add '+ ".jpg"' to the code.  Ex:

for name in list:
    arcpy.MakeRasterLayer_management(r"C:\ArcGISProjects\SamplePhotos" + "\\" + name + ".jpg", name)
0 Kudos
MeganMiller4
Deactivated User
This helped thanks.  I thought I tried that but I am assuming I had a mistype somewhere.  Thanks for all of your help.
0 Kudos