Mapping photos
March 25th, 2008 by Melaneum
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:
im=Image.open(folder+'/'+fileName) im.thumbnail(160,106)
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:
- kmlGen.py (modified from gpicsync)
- gmGeneration.py which does the job
Just edit the gmGeneration.py file to set the folder and url variables to whatever you need and run
python2.5 gmGeneration.py
EDIT 2008-04-03: Using libkml, the script is now simplified as shown on the new post “Mapping photo using libkml”.
This entry was posted on Tuesday, March 25th, 2008 at 21:29 CET and is filed under Geolocation, Python, Tagging, photo. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.