Skip to content

Instantly share code, notes, and snippets.

@andygarfield
Last active December 9, 2016 00:20
Show Gist options
  • Save andygarfield/551933ee18a7e51cff3660f8b8b3404b to your computer and use it in GitHub Desktop.
Save andygarfield/551933ee18a7e51cff3660f8b8b3404b to your computer and use it in GitHub Desktop.
Batch Convert Metadata to Excel
import exifread
import os
import pandas as pd
def convert_metatag_to_decimal(metatag_coordinate, direction_reference=None):
"""
Converts funky exif number format to float number
:param metatag_coordinate: Metatag coordinate (ie "[28, 120289/10000, 0]")
:param direction_reference: "N", "S", "E", or "W"
:return: float number
"""
coordinate_list = str(metatag_coordinate)[1:-1].split(', ')
decimal_coordinates = float(coordinate_list[0] + str(float(coordinate_list[1].split('/')[0]) /
float(coordinate_list[1].split('/')[1]) / 60)[1:])
if direction_reference and (str(direction_reference) == 'S' or str(direction_reference) == 'W'):
decimal_coordinates *= -1
return decimal_coordinates
file_path = os.path.dirname(__file__)
file_list = os.listdir(file_path)
# Get list of exif data tags
tag_list = []
for file_name in file_list:
if file_name.split(".")[-1] == 'JPG' or file_name.split(".")[-1] == 'jpg':
with open('{0}\\{1}'.format(file_path, file_name), 'rb') as picture:
try:
tags = exifread.process_file(picture)
except TypeError:
continue
for tag in tags:
if tag != 'JPEGThumbnail':
tag_list.append(str(tag))
break
# List for pandas.DataFrame objects to concatenate later
dataframe_list = []
# Iterate through photos
for file_name in file_list:
if file_name.split(".")[-1] == 'JPG' or file_name.split(".")[-1] == 'jpg':
with open('{0}\\{1}'.format(file_path, file_name), 'rb') as picture:
print "Working on", file_name
try:
tags = exifread.process_file(picture)
except TypeError:
continue
value_list = [str(tags[tag]) for tag in tag_list if tag != 'file_name']
new_row = pd.DataFrame([value_list], columns=tag_list, index=[file_name])
# Convert values to decimal
new_row.at[file_name, 'GPS GPSLatitude'] = \
convert_metatag_to_decimal(new_row.loc[file_name, 'GPS GPSLatitude'],
new_row.loc[file_name, 'GPS GPSLatitudeRef'])
new_row.at[file_name, 'GPS GPSLongitude'] = \
convert_metatag_to_decimal(new_row.loc[file_name, 'GPS GPSLongitude'],
new_row.loc[file_name, 'GPS GPSLongitudeRef'])
if len(new_row.loc[file_name, 'GPS GPSAltitude'].split('/')) > 1:
fraction = new_row.loc[file_name, 'GPS GPSAltitude'].split('/')
new_row.at[file_name, 'GPS GPSAltitude'] = float(fraction[0]) / float(fraction[1])
else:
new_row.at[file_name, 'GPS GPSAltitude'] = float(new_row.loc[file_name, 'GPS GPSAltitude'])
dataframe_list.append(new_row)
final_frame = pd.concat(dataframe_list)
final_frame.index.name = 'FileName'
writer = pd.ExcelWriter('{0}\\AllTags.xlsx'.format(file_path), engine='xlsxwriter')
final_frame.sort_index(axis=1).to_excel(writer, sheet_name='PhotoData')
writer.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment