Skip to content

Instantly share code, notes, and snippets.

@johanmeiring
Last active August 29, 2015 14:06
Show Gist options
  • Save johanmeiring/9b11741bbea0d0cd760a to your computer and use it in GitHub Desktop.
Save johanmeiring/9b11741bbea0d0cd760a to your computer and use it in GitHub Desktop.
Walk through dir, collect JPGs, copy to destination with timestamp as name, with duplicate detection.
import exifread
import os
import re
import shutil
in_dir = "path"
out_dir = "path"
clean = False
def do_the_walk(base_dir, out_dir):
for root, dirs, files in os.walk(base_dir):
for sub_dir in dirs:
do_the_walk(sub_dir, out_dir)
for the_file in files:
if the_file.upper().endswith('.JPG'):
f = open(root + "/" + the_file, 'rb')
tags = exifread.process_file(f)
if 'EXIF DateTimeOriginal' in tags:
tmp = str(tags['EXIF DateTimeOriginal'])
ts = re.sub(r'[^0-9]', '', tmp)
counter = 0
while True:
out_name = "%s_%s.JPG" % (ts, str(counter))
counter = counter + 1
if not os.path.isfile(out_dir + out_name):
break
print "%s - %s" % (the_file, out_name)
shutil.copyfile(root + "/" + the_file, out_dir + out_name)
def main():
if clean:
filelist = [f for f in os.listdir(out_dir)]
for f in filelist:
os.remove(out_dir + f)
do_the_walk(in_dir, out_dir)
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment