From 783590745eeed676118fdd19939d2d582e86924d Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Mon, 22 Jan 2018 16:24:02 +1100 Subject: [PATCH] Check image format before reading EXIF data --- scripts/photo_to_kml.py | 44 +++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/scripts/photo_to_kml.py b/scripts/photo_to_kml.py index 56f36a4..0a140eb 100644 --- a/scripts/photo_to_kml.py +++ b/scripts/photo_to_kml.py @@ -94,29 +94,24 @@ def export_kml_file(dirname, fnames, kml_name): kml = simplekml.Kml() 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)) + print('Reading {}...'.format(fname)) + with Image.open(os.path.join(dirname, fname)) as image: + exif_data = get_exif_data(image) + + 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) kml.save(kml_name) @@ -128,8 +123,9 @@ def main(): args = parser.parse_args() # Get files in image directory + ext = ['.jpg', '.jpeg', '.tif', '.tiff'] dirname = args.folder - fnames = os.listdir(dirname) + fnames = [f for f in os.listdir(dirname) if os.path.splitext(f)[-1] in ext] # Create kml file kml_name = dirname + '.kml'