Monday, 18 March 2013

Putting geotagged photos into Google Earth with Python

The final product. You click on a pin and a balloon with the
image taken at that location pops up.
So I got a bunch of pictures at work that turned out to be Geotagged. "Pretty cool" I thought to myself, "but clearly I need to see these on a map to know where they were taken!" Deciding to leap before actually looking, I missed the fact that Picassa can generate KMZ files for use in Google Earth directly, and instead went about making a script to do it manually. Doing it like this does have the slight advantage of being able to control the output KMZ much more precisely - so you can have bigger pictures or different icons or show different info in the balloons.

To do this, I used a couple of third party libraries for Python. Simplekml is a package that lets you write kml and kmz files. Although there were a couple of things that seemed to be bugs, it worked pretty well.
To extract the GPS coordinates, I needed a package to read exif data. At first I tried to use PIL, but I couldn't get it to work on my work computer (didn't have a c compiler set up correctly) so I ended up using pyexiv2. The page said it was deprecated and I should use Gexiv2 instead. That seemed to add more complications, so I just used pyexiv and it worked fine (note I did this in Python 2.7, if I had used Python 3.x pyexiv2 wouldn't have worked).

This outputs a KMZ file which includes all the pictures zipped up within it. In other words, it's making a copy of all your pictures in a file that could get pretty big if you have lots of them. Anyway, without further ado, here is the script that goes through a given folder and generates a kmz file with the locations of each.