-
Notifications
You must be signed in to change notification settings - Fork 0
/
import_photo.py
72 lines (55 loc) · 2.58 KB
/
import_photo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/python
import argparse
import datetime
import glob
import json
import os
import shutil
import exifread
from utils import files
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Import photos (*.jpg; *.jpeg) into specified folder organizing them according to EXIF DateTimeOriginal tag")
parser.add_argument('--src-dir', type=str,
help='directory containing photos (*.jpg; *.jpeg)')
parser.add_argument('--out-dir', type=str,
help='destination directory')
parser.add_argument('--log-file', type=str,
help='output file for imported photos list')
args = parser.parse_args()
log_file = args.log_file
out_dir = args.out_dir
src_dir = args.src_dir
imported = []
for src_file in glob.glob(os.path.join(src_dir, '**/*.*'), recursive=True):
if not src_file.lower().endswith('.jpg') and not src_file.lower().endswith('.jpeg'):
continue
try:
with open(src_file, 'rb') as file:
tags = exifread.process_file(file, details=False)
tag = None
if 'EXIF DateTimeOriginal' in tags.keys():
tag = tags['EXIF DateTimeOriginal']
elif 'EXIF DateTimeDigitized' in tags.keys():
tag = tags['EXIF DateTimeDigitized']
if not tag:
print('[ERROR] Creation time not found in {0}'.format(src_file))
continue
try:
dt = datetime.datetime.strptime(str(tag).split(' ')[0], '%Y:%m:%d')
dst_dir = os.path.join(out_dir, dt.strftime('%Y-%m-%d'))
os.makedirs(dst_dir, exist_ok=True)
dst_file = os.path.join(dst_dir, os.path.basename(src_file))
if os.path.exists(dst_file):
if files.equal(src_file, dst_file):
continue
if os.path.getsize(src_file) < os.path.getsize(dst_file):
continue
shutil.copy(src_file, dst_dir)
imported.append(os.sep.join(
[dt.strftime('%Y-%m-%d'), os.path.basename(dst_file)]))
except ValueError:
print('[ERROR] ValueError {0}'.format(src_file))
except PermissionError:
print('[ERROR] PermissionError {0}'.format(src_file))
with open(log_file, 'w', encoding='utf-8') as logfile:
json.dump({'files': imported}, logfile, ensure_ascii=False, indent=4)