-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdateTree.py
More file actions
executable file
·91 lines (68 loc) · 2.67 KB
/
dateTree.py
File metadata and controls
executable file
·91 lines (68 loc) · 2.67 KB
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python -B
# vi: set syntax=python ts=4 sw=4 sts=4 et ff=unix ai si :
#
# (c) Steven Scholnick <scholnicks@gmail.com>
# The dateTree source code is published under a MIT license.
"""
dateTree: Creates a tree of pictures separated into date directories
All duplicates will be removed.
All filenames will be converted to all lowercase
Only JPEGs will be copied
Usage:
dateTree [--quiet] <starting-directory> <destination-directory>
Requires external modules that can be installed from PyPI with:
pip install pillow
Options:
-h, --help Show this help screen
-q, --quiet Quiet Mode
--version Prints the version
"""
import datetime
import os
import shutil
import sys
from PIL import Image
from PIL.ExifTags import TAGS
DATE_FORMAT = "%Y-%m-%d"
def main(startingDirectory):
try:
pictureFiles = getPictureFiles(os.path.abspath(startingDirectory))
destinationRoot = createDirectory(arguments["<destination-directory>"])
for p in pictureFiles:
if not arguments["--quiet"]:
print("Processing {0}".format(p))
directoryName = getDate(p)
destinationDirectory = createDirectory(os.path.join(destinationRoot, directoryName), True)
shutil.copyfile(p, os.path.join(destinationDirectory, os.path.basename(p)))
except KeyboardInterrupt:
print()
sys.exit(0)
def getPictureFiles(sourceDirectory):
"""Returns a list of all of the JPEGs for the sourceDirectory and any sub-directories"""
pictureFiles = []
for root, dirs, files in os.walk(sourceDirectory):
pictureFiles += [os.path.join(root, f) for f in files if f.lower().endswith(".jpg")]
return pictureFiles
def getDate(filePath):
"""Returns the date for the specified file"""
# first try reading the creation date from the image itself
for k, v in Image.open(filePath)._getexif().iteritems():
if TAGS[k] == "DateTimeOriginal":
dateTime = datetime.datetime.strptime(v, "%Y:%m:%d %H:%M:%S")
return dateTime.strftime(DATE_FORMAT)
# unable to get the date from the image, just return the file's modification date
return datetime.datetime.fromtimestamp(os.path.getmtime(filePath)).strftime(DATE_FORMAT)
def createDirectory(path, allowExisting=False):
try:
os.mkdir(path)
os.chmod(path, 0o755)
return os.path.abspath(path)
except OSError:
if allowExisting:
return os.path.abspath(path)
else:
raise
if __name__ == "__main__":
from docopt import docopt
arguments = docopt(__doc__, version="1.0.1")
main(arguments["<starting-directory>"])