def getExifCreationDate(path):
  """Gets the earliest date from the file's EXIF header, returns time tuple"""
  timeStamp = None
  try:
    import exif
    pf = exif.parse(path)
    originalTime = pf.get('DateTimeOriginal')
    if (originalTime):
      timeStamp = time.strptime(originalTime, '%Y:%m:%d %H:%M:%S')
  except:
    pass
  
  #sometimes exif lib failes to retrieve data
  if (not timeStamp):
    response = os.popen(__path_to_exif + ' -x "%s"' % path, 'r')
    lines = response.read()
    matches = re.findall('<Date_and_Time.+?>(.*?)</Date_and_Time.+?>', lines)
    if (len(matches)):
      timeStamp = min(*[time.strptime(x, '%Y:%m:%d %H:%M:%S') for x in matches])
  return timeStamp
Gets the earliest date from the file's EXIF header, returns time tuple
recipes/Python/576822_Redate_pictures_with_EXIF/recipe-576822.py