-
Notifications
You must be signed in to change notification settings - Fork 3
/
copyimages.py
60 lines (45 loc) · 1.63 KB
/
copyimages.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
# coding:utf-8
import os
import shutil
import sys
from datetime import datetime
import exifread
from constants import (
DATABASE_PHOTOS_PATH,
POSSIBLE_STORAGE_PATHS,
SYSTEM_PREPATH
)
def import_images(source_path):
""" Find attached storage and run importing all existing images """
copied, total = 0, 0
for r, d, files in os.walk(source_path):
images = filter(lambda x: x.upper().endswith('JPG'), files)
for image in images:
total += 1
with open(os.path.join(source_path, r, image), 'rb') as f:
try:
created = str(exifread.process_file(f)['Image DateTime'])
except:
continue
d = datetime.strptime(created.split()[0], '%Y:%m:%d')
targetpath = os.path.join(
DATABASE_PHOTOS_PATH, '{:02}'.format(d.month),
'{:02}'.format(d.day))
target = os.path.join(targetpath, image)
if not os.path.exists(target):
try:
os.makedirs(targetpath)
except:
pass
shutil.copyfile(os.path.join(source_path, r, image), target)
copied += 1
sys.stdout.write('.')
if __name__ == '__main__':
source_path = None
for each in POSSIBLE_STORAGE_PATHS:
if os.path.exists(SYSTEM_PREPATH.format(each)):
source_path = SYSTEM_PREPATH.format(each)
break
if not source_path:
raise ValueError('Photos source path not found')
import_images(source_path)