You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
169 lines
4.6 KiB
Python
169 lines
4.6 KiB
Python
7 years ago
|
"""Add geotagged images to a kml for viewing in Google Earth.
|
||
|
|
||
7 years ago
|
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.
|
||
7 years ago
|
|
||
7 years ago
|
Usage:
|
||
|
photo_to_kml.py [-h] IMAGE_FOLDER
|
||
|
|
||
|
Positional arguments:
|
||
|
IMAGE_FOLDER name of input folder
|
||
|
|
||
|
Optional arguments:
|
||
|
-h, --help show this help message and exit
|
||
|
|
||
7 years ago
|
Examples:
|
||
|
|
||
7 years ago
|
Create a kml for a folder of photos, named 'image_folder'.
|
||
|
> python photo_to_kml.py image_folder
|
||
7 years ago
|
|
||
|
"""
|
||
|
|
||
|
__author__ = "D. Howe"
|
||
|
__version__ = "0.1.0"
|
||
|
__email__ = "d.howe@wrl.unsw.edu.au"
|
||
|
|
||
7 years ago
|
import os
|
||
|
import argparse
|
||
7 years ago
|
from PIL import Image
|
||
|
from PIL.ExifTags import TAGS, GPSTAGS
|
||
7 years ago
|
import simplekml
|
||
7 years ago
|
|
||
|
|
||
|
def get_exif_data(image):
|
||
|
"""
|
||
|
Return a dictionary from the exif data of an PIL Image object, and convert
|
||
|
the GPS uags.
|
||
|
https://gist.github.com/valgur/2fbed04680864fab1bfc
|
||
|
"""
|
||
|
|
||
|
info = image._getexif()
|
||
|
if not info:
|
||
|
return {}
|
||
|
exif_data = {TAGS.get(tag, tag): value for tag, value in info.items()}
|
||
|
|
||
|
def is_fraction(val):
|
||
|
return isinstance(val, tuple) and len(val) == 2 and isinstance(
|
||
|
val[0], int) and isinstance(val[1], int)
|
||
|
|
||
|
def frac_to_dec(frac):
|
||
|
return float(frac[0]) / float(frac[1])
|
||
|
|
||
|
if 'GPSInfo' in exif_data:
|
||
7 years ago
|
gpsinfo = {
|
||
|
GPSTAGS.get(t, t): v
|
||
|
for t, v in exif_data['GPSInfo'].items()
|
||
|
}
|
||
7 years ago
|
for tag, value in gpsinfo.items():
|
||
|
if is_fraction(value):
|
||
|
gpsinfo[tag] = frac_to_dec(value)
|
||
|
elif all(is_fraction(x) for x in value):
|
||
|
gpsinfo[tag] = tuple(map(frac_to_dec, value))
|
||
|
exif_data['GPSInfo'] = gpsinfo
|
||
|
|
||
|
return exif_data
|
||
|
|
||
|
|
||
|
def get_lat_lon(exif_data):
|
||
|
"""
|
||
|
Return the latitude and longitude (if available) from exif_data.
|
||
|
https://gist.github.com/valgur/2fbed04680864fab1bfc
|
||
|
"""
|
||
|
lat = None
|
||
|
lon = None
|
||
|
gps_info = exif_data.get('GPSInfo')
|
||
|
|
||
|
def convert_to_degrees(value):
|
||
|
d, m, s = value
|
||
|
return d + (m / 60.0) + (s / 3600.0)
|
||
|
|
||
|
if gps_info:
|
||
|
gps_latitude = gps_info.get('GPSLatitude')
|
||
|
gps_latitude_ref = gps_info.get('GPSLatitudeRef')
|
||
|
gps_longitude = gps_info.get('GPSLongitude')
|
||
|
gps_longitude_ref = gps_info.get('GPSLongitudeRef')
|
||
|
|
||
7 years ago
|
if (gps_latitude and gps_latitude_ref and gps_longitude and
|
||
|
gps_longitude_ref):
|
||
7 years ago
|
lat = convert_to_degrees(gps_latitude)
|
||
|
if gps_latitude_ref == 'S':
|
||
|
lat = -lat
|
||
|
|
||
|
lon = convert_to_degrees(gps_longitude)
|
||
|
if gps_longitude_ref == 'W':
|
||
|
lon = -lon
|
||
|
|
||
|
return lat, lon
|
||
|
|
||
|
|
||
7 years ago
|
def export_kml_file(dirname, fnames, dirname_thumbs, kml_name):
|
||
7 years ago
|
"""
|
||
|
Create the kml document
|
||
|
"""
|
||
|
kml = simplekml.Kml()
|
||
|
|
||
7 years ago
|
for fname in fnames:
|
||
7 years ago
|
print('Reading {}...'.format(fname))
|
||
|
with Image.open(os.path.join(dirname, fname)) as image:
|
||
7 years ago
|
# Get EXIF tags
|
||
7 years ago
|
exif_data = get_exif_data(image)
|
||
|
|
||
7 years ago
|
# Save thumbnail
|
||
|
image.thumbnail((128, 128))
|
||
|
image.save(os.path.join(dirname_thumbs, fname), 'jpeg')
|
||
|
|
||
7 years ago
|
lat, lon = get_lat_lon(exif_data)
|
||
|
pnt = kml.newpoint(name=fname)
|
||
|
pnt.coords = [(lon, lat)]
|
||
|
|
||
7 years ago
|
# Add popup window with full-size image
|
||
7 years ago
|
pnt.description = (
|
||
|
'<![CDATA[ <img src="{}"" height="500px" />]]>'.format(
|
||
|
os.path.join(dirname, fname)))
|
||
7 years ago
|
|
||
|
# Show groups of photos as camera icon
|
||
7 years ago
|
pnt.stylemap.normalstyle.iconstyle.scale = 1
|
||
|
pnt.stylemap.normalstyle.iconstyle.icon.href = (
|
||
|
'http://maps.google.com/'
|
||
|
'mapfiles/kml/shapes/camera.png')
|
||
7 years ago
|
|
||
|
# Show placemark as image thumbnail
|
||
|
pnt.stylemap.highlightstyle.iconstyle.scale = 3
|
||
7 years ago
|
pnt.stylemap.highlightstyle.iconstyle.icon.href = os.path.join(
|
||
7 years ago
|
dirname_thumbs, fname)
|
||
7 years ago
|
|
||
|
kml.save(kml_name)
|
||
|
|
||
|
|
||
|
def main():
|
||
7 years ago
|
parser = argparse.ArgumentParser()
|
||
7 years ago
|
parser.add_argument(
|
||
7 years ago
|
'folder',
|
||
|
metavar='IMAGE_FOLDER',
|
||
|
help='name of input folder',
|
||
|
default=None)
|
||
7 years ago
|
args = parser.parse_args()
|
||
|
|
||
|
# Get files in image directory
|
||
7 years ago
|
ext = ['.jpg', '.jpeg', '.tif', '.tiff']
|
||
7 years ago
|
dirname = args.folder
|
||
7 years ago
|
fnames = [
|
||
|
f for f in os.listdir(dirname)
|
||
|
if os.path.splitext(f)[-1].lower() in ext
|
||
|
]
|
||
7 years ago
|
|
||
7 years ago
|
# Create thumbnails folder beside originals
|
||
|
dirname_thumbs = os.path.join(os.path.dirname(dirname), 'thumbs')
|
||
7 years ago
|
try:
|
||
|
os.mkdir(dirname_thumbs)
|
||
|
except FileExistsError:
|
||
|
pass
|
||
|
|
||
7 years ago
|
# Create kml file
|
||
|
kml_name = dirname + '.kml'
|
||
7 years ago
|
export_kml_file(dirname, fnames, dirname_thumbs, kml_name)
|
||
7 years ago
|
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
main()
|