Tidy up relative file names in kml

master
Dan Howe 7 years ago
parent ce0af660c7
commit 0f7d9a05d2

@ -1,13 +1,10 @@
"""Add geotagged images to a kml for viewing in Google Earth. """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 This script takes a folder of images, and creates a placemark with a thumbnail
with a thumbnail of that image, based on the GPS coordinates in the EXIF tags. of each image, based on the GPS coordinates in the EXIF tags.
Examples: 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'. Create a kml for a folder of photos, named 'images'.
> python photo_to_kml.py images > python photo_to_kml.py images
@ -17,11 +14,11 @@ __author__ = "D. Howe"
__version__ = "0.1.0" __version__ = "0.1.0"
__email__ = "d.howe@wrl.unsw.edu.au" __email__ = "d.howe@wrl.unsw.edu.au"
import sys import os
import glob import argparse
import simplekml
from PIL import Image from PIL import Image
from PIL.ExifTags import TAGS, GPSTAGS from PIL.ExifTags import TAGS, GPSTAGS
import simplekml
def get_exif_data(image): def get_exif_data(image):
@ -44,8 +41,10 @@ def get_exif_data(image):
return float(frac[0]) / float(frac[1]) return float(frac[0]) / float(frac[1])
if 'GPSInfo' in exif_data: if 'GPSInfo' in exif_data:
gpsinfo = {GPSTAGS.get(t, t): v gpsinfo = {
for t, v in exif_data['GPSInfo'].items()} GPSTAGS.get(t, t): v
for t, v in exif_data['GPSInfo'].items()
}
for tag, value in gpsinfo.items(): for tag, value in gpsinfo.items():
if is_fraction(value): if is_fraction(value):
gpsinfo[tag] = frac_to_dec(value) gpsinfo[tag] = frac_to_dec(value)
@ -88,49 +87,53 @@ def get_lat_lon(exif_data):
return lat, lon return lat, lon
def export_kml_file(file_names, kml_name): def export_kml_file(dirname, fnames, kml_name):
""" """
Create the kml document Create the kml document
""" """
kml = simplekml.Kml() kml = simplekml.Kml()
for file_name in file_names: for fname in fnames:
try:
print('Reading ' + file_name + '...') with Image.open(os.path.join(dirname, fname)) as image:
with Image.open(file_name) as image:
exif_data = get_exif_data(image) exif_data = get_exif_data(image)
lat, lon = get_lat_lon(exif_data) print('Reading {}...'.format(fname))
pnt = kml.newpoint(name=file_name) lat, lon = get_lat_lon(exif_data)
pnt = kml.newpoint(name=fname)
pnt.coords = [(lon, lat)] pnt.coords = [(lon, lat)]
# Add comtent to popup window # Add content to popup window
pnt.description = ( pnt.description = (
'<![CDATA[ <img src=' + file_name + ' height="500px" />]]>') '<![CDATA[ <img src={} height="500px" />]]>'.format(
os.path.join(dirname, fname)))
pnt.stylemap.normalstyle.iconstyle.scale = 1 pnt.stylemap.normalstyle.iconstyle.scale = 1
pnt.stylemap.normalstyle.iconstyle.icon.href = ( pnt.stylemap.normalstyle.iconstyle.icon.href = (
'http://maps.google.com/' 'http://maps.google.com/'
'mapfiles/kml/shapes/camera.png') 'mapfiles/kml/shapes/camera.png')
pnt.stylemap.highlightstyle.iconstyle.scale = 2 pnt.stylemap.highlightstyle.iconstyle.scale = 2
pnt.stylemap.highlightstyle.iconstyle.icon.href = file_name 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) kml.save(kml_name)
def main(): def main():
parser = argparse.ArgumentParser(usage=__doc__) parser = argparse.ArgumentParser(usage=__doc__)
parser.add_argument('-f', parser.add_argument(
'--file', 'folder', nargs='?', help='name of input folder', default=None)
help='name of parameter file', args = parser.parse_args()
default=None)
# Get files in image directory
if len(sys.argv) == 1: dirname = args.folder
file_names = glob.glob('*.jpg') fnames = os.listdir(dirname)
else:
file_names = glob.glob(sys.argv[1]) # Create kml file
export_kml_file(file_names, 'output.kml') kml_name = dirname + '.kml'
export_kml_file(dirname, fnames, kml_name)
if __name__ == '__main__': if __name__ == '__main__':

Loading…
Cancel
Save