Skip to content

Instantly share code, notes, and snippets.

@jsonbecker
Last active January 2, 2016 00:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jsonbecker/8224317 to your computer and use it in GitHub Desktop.
Save jsonbecker/8224317 to your computer and use it in GitHub Desktop.
A few versions of getting date data from photos.
import sys
import os, shutil, time
import subprocess
import os.path
import exifread
from datetime import datetime
def photoDateSIPS(f):
"Return the date/time on which the given photo was taken."
cDate = subprocess.check_output(['sips', '-g', 'creation', f])
cDate = cDate.split('\n')[1].lstrip().split(': ')[1]
return datetime.strptime(cDate, "%Y:%m:%d %H:%M:%S")
def photoDateMod(f):
"Return the date/time the photo was last modified."
cDate = time.ctime(os.path.getmtime(f))
return datetime.strptime(cDate, "%a %b %d %H:%M:%S %Y")
def photoDateEXIF(f):
"Return date time based on the EXIF data DateTimeOriginal"
f = open(f, 'rb')
tags = exifread.process_file(f)
try:
cDate = str(tags['EXIF DateTimeOriginal'])
except KeyError:
try:
cDate = str(tags['Image DateTime'])
except:
return Exception
f.close()
try:
output = datetime.strptime(cDate, "%Y:%m:%d %H:%M:%S")
except:
return Exception
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment