If you’ve followed earlier intructions on tagging your photos, you now have a set of photos with the gps location information on the exif tag. As we have seen on a previous article on blog-mapping, it is pretty easy to insert Google maps on a website.
We will now see how to make the generation of photo map automatic with python from this set of geotagged photos. We need to generate thumbnails of the images and the kml file. Here is the idea behind the script:
- Loop through all jpeg files in a folder (we assume the selection is already done and that you have only the photos you want to put on the map)
- Read the exif informations from the file
- Generate a resized version of the image adapted to browser visualization (800 pixels) and a thumbnail (160 pixels).
- Write the corresponding instructions in the KML file
Looping through all the jpeg files in the folder is the easy part with python (
for fileName in os.listdir( folder ): combine with a matching based on
fnmatch.fnmatch (fileName, '*.jpg')).
Reading the information is done using the pyexiv2. For example:
image = pyexiv2.Image(filename) image.readMetadata() orientation=image['Exif.Image.Orientation']
We can take advantage of the powerfull PIL library to resize the images:
As we had easily retrieved the orientation information from the exif data, we can rotate the photos:
im=im.rotate(90). I choose to prefix the 800pix filename with ‘w’ and the 160pix filename with ‘_thb_w’ (to match my earlier SPGM gallery).
Intructions in the KML file look like that:
<placemark> <name>Photo Name</name> <description> <!--[CDATA[<a href='http://www.website.sth/wherethefileis.jpg' target='_blank'> <img src='http://www.website.sth/wherethethumbnailis.jpg' /> </a>]]--></description> <styleurl>#camera</styleurl> <point> <coordinates>-159.432961,21.875480,11.0</coordinates> </point> </placemark>
To make this output effortlessly (almost), I just used a python file from gpicsync which is very nice but lack the feature of using directly the GPS information from the photos.
Here is the result:
The full script is just two python files:
Just edit the gmGeneration.py file to set the
url variables to whatever you need and run
EDIT 2008-04-03: Using libkml, the script is now simplified as shown on the new post “Mapping photo using libkml”.
EDIT 2009-08-30: after few updates due to my trip in South Africa (south hemisphere), i’ve updated the script and find more convenient to put it on http://code.google.com/p/jpegtokml/.