Created
July 28, 2020 17:58
-
-
Save chazcheadle/4d23d3138923c4ce1f000d46f5744086 to your computer and use it in GitHub Desktop.
Write csv of lat/lon from directory of images
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import csv | |
import os | |
import exifread | |
# Prompt for directory to read from. | |
dir_name = input("Enter the directory to search [./]: ") | |
csv_file = input("Enter name for output file [output.csv]: ") | |
if dir_name == '': | |
dir_name = './' | |
if csv_file == '': | |
csv_file = 'output.csv' | |
def _convert_to_degress(value): | |
""" | |
Helper function to convert the GPS coordinates stored in the EXIF to degress in float format | |
:param value: | |
:type value: exifread.utils.Ratio | |
:rtype: float | |
""" | |
d = float(value.values[0].num) / float(value.values[0].den) | |
m = float(value.values[1].num) / float(value.values[1].den) | |
s = float(value.values[2].num) / float(value.values[2].den) | |
return d + (m / 60.0) + (s / 3600.0) | |
directory = os.listdir(dir_name) | |
with open(csv_file, 'w', newline='') as csv_file: | |
csvwriter = csv.writer(csv_file) | |
for files in directory: | |
if files.endswith (('jpg','JPG','png','PNG','tiff','TIFF')): | |
file_name = os.path.join(dir_name, files) | |
with open(file_name, 'rb') as file: | |
tags = exifread.process_file(file) | |
latitude = tags.get('GPS GPSLatitude') | |
latitude_ref = tags.get('GPS GPSLatitudeRef') | |
longitude = tags.get('GPS GPSLongitude') | |
longitude_ref = tags.get('GPS GPSLongitudeRef') | |
print(f'{file_name}, {_convert_to_degress(latitude)}, {_convert_to_degress(longitude)}') | |
csvwriter.writerow([file_name, _convert_to_degress(latitude), _convert_to_degress(longitude)]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment