From 0f7d9a05d2c0dfe0b3033c9fbd3a2796838502dc Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Mon, 22 Jan 2018 16:03:21 +1100 Subject: [PATCH] Tidy up relative file names in kml --- scripts/photo_to_kml.py | 87 +++++++++++++++++++++-------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/scripts/photo_to_kml.py b/scripts/photo_to_kml.py index bc43b57..56f36a4 100644 --- a/scripts/photo_to_kml.py +++ b/scripts/photo_to_kml.py @@ -1,13 +1,10 @@ """Add geotagged images to a kml for viewing in Google Earth. -This script takes an image (or a folder of images), and creates a placemark -with a thumbnail of that image, based on the GPS coordinates in the EXIF tags. +This script takes a folder of images, and creates a placemark with a thumbnail +of each image, based on the GPS coordinates in the EXIF tags. Examples: -Create a kml for one photo. - > python photo_to_kml.py IMG_001100.jpg - Create a kml for a folder of photos, named 'images'. > python photo_to_kml.py images @@ -17,11 +14,11 @@ __author__ = "D. Howe" __version__ = "0.1.0" __email__ = "d.howe@wrl.unsw.edu.au" -import sys -import glob -import simplekml +import os +import argparse from PIL import Image from PIL.ExifTags import TAGS, GPSTAGS +import simplekml def get_exif_data(image): @@ -44,8 +41,10 @@ def get_exif_data(image): return float(frac[0]) / float(frac[1]) if 'GPSInfo' in exif_data: - gpsinfo = {GPSTAGS.get(t, t): v - for t, v in exif_data['GPSInfo'].items()} + gpsinfo = { + GPSTAGS.get(t, t): v + for t, v in exif_data['GPSInfo'].items() + } for tag, value in gpsinfo.items(): if is_fraction(value): gpsinfo[tag] = frac_to_dec(value) @@ -88,49 +87,53 @@ def get_lat_lon(exif_data): return lat, lon -def export_kml_file(file_names, kml_name): +def export_kml_file(dirname, fnames, kml_name): """ Create the kml document """ kml = simplekml.Kml() - for file_name in file_names: - - print('Reading ' + file_name + '...') - - with Image.open(file_name) as image: - exif_data = get_exif_data(image) - - lat, lon = get_lat_lon(exif_data) - - pnt = kml.newpoint(name=file_name) - pnt.coords = [(lon, lat)] - - # Add comtent to popup window - pnt.description = ( - ']]>') - pnt.stylemap.normalstyle.iconstyle.scale = 1 - pnt.stylemap.normalstyle.iconstyle.icon.href = ( - 'http://maps.google.com/' - 'mapfiles/kml/shapes/camera.png') - pnt.stylemap.highlightstyle.iconstyle.scale = 2 - pnt.stylemap.highlightstyle.iconstyle.icon.href = file_name + for fname in fnames: + try: + with Image.open(os.path.join(dirname, fname)) as image: + exif_data = get_exif_data(image) + + print('Reading {}...'.format(fname)) + + lat, lon = get_lat_lon(exif_data) + pnt = kml.newpoint(name=fname) + pnt.coords = [(lon, lat)] + + # Add content to popup window + pnt.description = ( + ']]>'.format( + os.path.join(dirname, fname))) + pnt.stylemap.normalstyle.iconstyle.scale = 1 + pnt.stylemap.normalstyle.iconstyle.icon.href = ( + 'http://maps.google.com/' + 'mapfiles/kml/shapes/camera.png') + pnt.stylemap.highlightstyle.iconstyle.scale = 2 + pnt.stylemap.highlightstyle.iconstyle.icon.href = os.path.join( + dirname, fname) + except OSError: + print('Skipping {} (not a valid image).'.format(fname)) kml.save(kml_name) def main(): parser = argparse.ArgumentParser(usage=__doc__) - parser.add_argument('-f', - '--file', - help='name of parameter file', - default=None) - - if len(sys.argv) == 1: - file_names = glob.glob('*.jpg') - else: - file_names = glob.glob(sys.argv[1]) - export_kml_file(file_names, 'output.kml') + parser.add_argument( + 'folder', nargs='?', help='name of input folder', default=None) + args = parser.parse_args() + + # Get files in image directory + dirname = args.folder + fnames = os.listdir(dirname) + + # Create kml file + kml_name = dirname + '.kml' + export_kml_file(dirname, fnames, kml_name) if __name__ == '__main__':