Insert jpg with arcpy

4406
4
04-14-2015 02:33 AM
Yaron_YosefCohen
Occasional Contributor II

Hi,

Is it possible to insert a new jpg (as a picture and not as a layer) to several mxd's with arcpy?  i didn't see any reference in ArcGIS Help (10.2, 10.2.1, and 10.2.2)

Help would be greatly appreciated.

Tags (1)
0 Kudos
4 Replies
DavidBarnes
Occasional Contributor III

You can't insert new elements (of any kind) with arcpy, but you can modify existing ones. So what you could do is insert pictures through the ui, and place them somewhere off the page if you don't need them all of the time. Then, when you do need them you can move them onto the page with arpcy and change the picture's source path as well.

DarrenWiens2
MVP Honored Contributor

Warning: extremely convoluted method below (installing and configuring comtypes/Snippets can be painful), and technically, it doesn't use arcpy at all. But it does use Python

You can access most, if not all, ArcMap functionality using ArcObjects in Python. The code below adds 'happy.jpg' to an empty mxd's layout, but with a little tweaking, you could add it to whatever mxd you choose.

from comtypes.client import GetModule, CreateObject
from Snippets import GetLibPath, InitStandalone


def CType(obj, interface):
   """Casts obj to interface and returns comtypes POINTER or None"""
   try:
       newobj = obj.QueryInterface(interface)
       return newobj
   except:
       return None


def main():
    InitStandalone()
    modCarto = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriCarto.olb")
    modGeom = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriGeometry.olb")
    modArcMapUI = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriArcMapUI.olb")
    modFramework = GetModule(r"C:\Program Files (x86)\ArcGIS\Desktop10.2\com\esriFramework.olb")

    app = CreateObject(modFramework.AppROT, interface=modFramework.IAppROT)
    mxDoc = CType(app.Item(0).Document,modArcMapUI.IMxDocument)
    layout = mxDoc.PageLayout
    graphicContainer = CType(layout, modCarto.IGraphicsContainer)

    pictureElement = CreateObject(modCarto.JpgPictureElement, interface=modCarto.IPictureElement)

    envelope = CreateObject(modGeom.Envelope, interface=modGeom.IEnvelope)
    envelope.PutCoords(0, 0, 5, 5)

    pictureElement.ImportPictureFromFile(r'C:\junk\happy.jpg')
    iElement = CType(pictureElement,modCarto.IElement)
    iElement.Geometry = envelope

    graphicContainer.AddElement(iElement,0)

    app.Item(0).SaveDocument()

if __name__ == '__main__':
    main()
DavidBarnes
Occasional Contributor III

Good point, Darren!

(That part of things is so far over my head it's out of sight. It's like asking if you can fly to the moon. As a bird my answer is no. I guess it takes a rocket scientist. ha ha)

0 Kudos
DarrenWiens2
MVP Honored Contributor

Good analogy (the moon thing, not the rocket scientist thing haha), my answer is definitely overkill. For most people, adding a dummy image box is the way to go, rather than fighting with ArcObjects.

0 Kudos