Displaying local image in simplekml
Asked Answered
P

1

7

I'm trying to use simplekml to put a bunch of geotagged photographs into a KML file (well, a KMZ file actually) to view in Google Earth. I've gotten the locations to display, however when I try to put the image in the "description" so when I click on the locations the image comes up, it doesn't work. There is just a blank image. I'm trying to do it by using the addfile() command shown here. My code looks like this:

import os, simplekml

path = r'C:\Users\as\Desktop\testpics'                     
    
kml = simplekml.Kml()

for (dirpath, dirnames, filenames) in os.walk(path):
    for filename in filenames:
        fullpath = os.path.join(dirpath, filename)
        try:
            Lat, Long, Alt = GetLatLong(fullpath) #Didn't include this function, but it appears to work
        except:
            Lat, Long, Alt = (None, None, None)
        if Lat: #Only adds to kml if it has Lat value.
            x, y = (FormatLatLong(Lat), FormatLatLong(Long)) #puts into decimal coords
            point = kml.newpoint(name = filename , coords = [(y,x)])
            picpath = kml.addfile(fullpath)
            point.description = '<img src="' + picpath +'" alt="picture" width="400" height="300" align="left" />'

            

kml.savekmz("kmltest2.kmz", format = False)

As you can see, I've pretty much cut and paste the instructions for using "addfile" from the instructions on the page above. The point.description line seems to be where things are going wrong.

The pictures are getting added to the kmz archive, but they're not showing up in the location bubbles. I thought it might be because I'm doing this on Windows 7 and the slashes are backwards, but I tried manually changing the files\image.jpg to files/image.jpg and it didn't fix it. The produced KMZ doc.kml file looks like this:

    <kml xmlns="http://www.opengis.net/kml/2.2"xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_1">
    <Placemark id="feat_2">
    <name>DSC00001.JPG</name>
    <description>&lt;img src="files/DSC00001.JPG" alt="picture" width="400" height="300" align="left" /&gt;</description>
    <Point id="geom_0"><coordinates>18.9431816667,9.44355222222,0.0</coordinates>
    </Point></Document></kml>

(I've removed all but one of the points) Thanks a lot, Alex

Plump answered 12/3, 2013 at 23:4 Comment(1)
I also tried Option 1 here, didn't fix it.Plump
M
3

May be it's because of unclosed placemark tag in the kml file which you had written. So close the placemark tag after point tag closing.

<kml xmlns="http://www.opengis.net/kml/2.2"xmlns:gx="http://www.google.com/kml/ext/2.2">
    <Document id="feat_1">
    <Placemark id="feat_2">
    <name>DSC00001.JPG</name>
    <description>&lt;img src="files/DSC00001.JPG" alt="picture" width="400" height="300" align="left" /&gt;</description>
    <Point id="geom_0"><coordinates>18.9431816667,9.44355222222,0.0</coordinates>
    </Point></Placemark></Document></kml>

If the above code after placing place mark tag doesn't work then try with Balloon Style instead of description tag try with below code

<kml xmlns="http://www.opengis.net/kml/2.2"
     xmlns:gx="http://www.google.com/kml/ext/2.2" 
     xmlns:kml="http://www.opengis.net/kml/2.2" 
     xmlns:atom="http://www.w3.org/2005/Atom"
>
<Document id="feat_1">
<Placemark id="feat_2">
<name>DSC00001.JPG</name>
<Style>
<BalloonStyle>
<text><![CDATA[
 <table width=100% cellpadding=0 cellspacing=0>
  <tr><td><img width=100% src='files/DSC00001.jpg' /></td></tr></table>]]>
</text>
</BalloonStyle>
</Style> 
<Point id="geom_0">
<coordinates>18.9431816667,9.44355222222</coordinates>
</Point>
</Placemark>
</Document>
</kml>
Monachism answered 14/3, 2013 at 11:8 Comment(2)
Great, thanks a lot. I did have a </placemark> in my original file I just forgot to copy it when I trimmed it down, but your second suggestion did work. I added this to my simplekml script: point.style.balloonstyle.text = "<![CDATA[ <table width=100% cellpadding=0 cellspacing=0> <tr><td><img width=100% src='" + filename + "' /></td></tr></table>]]>" . Interesting note: the filename extension MUST be lower case even if the image isn't named that way - .JPG won't work, .jpg will.Plump
Now that I think of it, maybe the problem with the original script was that the images where all .JPG, not .jpg. I could go and test it, but it would mean undoing a bunch of changes, so I'm not going to.Plump

© 2022 - 2024 — McMap. All rights reserved.