-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathphotosort.py
executable file
·68 lines (54 loc) · 2.48 KB
/
photosort.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
#!/usr/bin/env python
import sys
import os
import argparse
import exifread
import jdatetime
from termcolor import colored, cprint
def senderror(errormsg, status=1):
sys.stderr.write(colored('ERORR: %s\n' % (errormsg), 'red', attrs=['bold']))
sys.exit(status)
parser = argparse.ArgumentParser(prog='photosort.py', usage='%(prog)s -o DIR [FILES...]', description='Sort images based on date value in EXIF data.')
parser.add_argument("files", nargs='+', help="list of files to be processed")
parser.add_argument("-o", "--out", required=True, help="output directory")
parser.add_argument("-n", "--noact", required=False, action="store_true", help="no action, just simulate")
args = parser.parse_args()
# Check output directory exists
if not os.path.isdir(args.out):
senderror('%s: No such file or directory' % (args.out))
# Check output directory is writable
if not os.access(args.out, os.W_OK):
senderror('%s: Permission denied' % (args.out))
for file in args.files:
try:
f = open(file, 'rb')
except IOError, Argument:
print colored('%s: %s' % (file, Argument.strerror), 'red')
continue
else:
tags = exifread.process_file(f)
if not len(tags):
print colored('%s: %s' % (file, 'No EXIF data found'), 'red')
continue
else:
tag = 'EXIF DateTimeOriginal'
if tag not in tags.keys():
print colored('%s: %s' % (file, 'Tag not found in EXIF'), 'red')
continue
rawdate = str(tags[tag]).split()[0].split(':')
jdate = jdatetime.date.fromgregorian(day=int(rawdate[2]),month=int(rawdate[1]),year=int(rawdate[0]))
dirname = "%s/%s/%s/%s" % (args.out, jdate.year, '%.2d' % jdate.month, '%.2d' % jdate.day)
if os.path.isfile('%s/%s' % (dirname,file.split('/')[-1])):
print colored(('%s: %s' % (file, jdate)).ljust(80, '.') + '[ DU ]', 'red')
continue
if not os.access(file, os.R_OK):
print colored(('%s: %s' % (file, jdate)).ljust(80, '.') + '[ NR ]', 'red')
continue
if not args.noact:
os.system('mkdir -p %s' % (dirname))
os.system('cp "%s" "%s"' % (file, dirname))
print colored(('%s: %s' % (file, jdate)).ljust(80, '.') + '[ OK ]', 'cyan')
#
# for tag in tags.keys():
# if tag not in ('JPEGThumbnail', 'TIFFThumbnail', 'Filename', 'EXIF MakerNote'):
# print "Key: %s, value %s" % (tag, tags[tag])