-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpicload.py
More file actions
62 lines (53 loc) · 1.99 KB
/
picload.py
File metadata and controls
62 lines (53 loc) · 1.99 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
#!/usr/bin/env python3
import exifread
import os
import shutil
from datetime import datetime
from optparse import OptionParser
def cutAndPasteToDir(filepath,outputdir,copy):
if not ( os.path.exists(outputdir) and os.path.isdir(outputdir)):
os.mkdir(outputdir)
if copy:
print(shutil.copy2(filepath,outputdir))
else:
print(shutil.move(filepath,outputdir, copy_function=shutil.copy2))
def getInputdir(path):
if path:
return os.path.abspath(path)
else:
return os.getcwd()
def getOutputdir(path):
if path:
return os.path.abspath(path)
else:
return os.getcwd()
def main():
usage = "usage: %prog [options] arg1 arg2\n The utility moves/copies pictures.\
\nCreates a folder with the name date from exif information."
p = OptionParser(usage=usage)
p.add_option("-i","--inputdir",None,help = "The folder from which the files will be moved")
p.add_option("-o","--outputdir",None,help = "The folder to which the files will be moved")
p.add_option("-c","--copy",action = "store_true",default=False,help = "Copied files true")
option, arguments = p.parse_args()
inputdir = getInputdir(option.inputdir)
outputdir = getOutputdir(option.outputdir)
print("dir path:",inputdir)
listfiles = os.listdir(path=inputdir)
print("Files and folders detected:",len(listfiles))
count = 0;
for f in listfiles[:]:
pf = os.path.join(inputdir,f)
if os.path.isfile(pf):
of = open(pf,"rb")
tg = exifread.process_file(of)
if tg.setdefault("Image DateTime",False):
dt = datetime.strptime(tg.setdefault("Image DateTime").values,"%Y:%m:%d %H:%M:%S")
subdir = os.path.join(outputdir,str(dt.date()))
cutAndPasteToDir(pf,subdir,option.copy)
count +=1
else:
listfiles.remove(f)
print("Processed files:",len(listfiles))
print("Moved/Copied files:",count)
if __name__ == '__main__':
main()