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.
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:
for fname in fnames:
try:
with Image.open(os.path.join(dirname, fname)) as 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)]
# Add comtent to popup window
# Add content to popup window
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.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
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__':

Loading…
Cancel
Save