-
Notifications
You must be signed in to change notification settings - Fork 0
/
gallery.py
executable file
·69 lines (53 loc) · 1.93 KB
/
gallery.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
#!/usr/bin/python
import commands
import os
import sys
import argparse
import re
import shutil
def main():
# get input arguments
parser = argparse.ArgumentParser()
parser.add_argument('-s', '--size', default='1280x800')
parser.add_argument('-q', '--quality', default=85, type=int)
parser.add_argument('-v', '--verbose', action='store_true')
parser.add_argument('directory')
args = parser.parse_args()
# define directory structur
dirSource = os.path.abspath( args.directory )
dirDestCR2 = dirSource + '/CR2'
dirDestWeb = dirSource + '/websize'
# check if source directory exists
if not os.path.exists(dirSource):
print args.directory, "does not exist"
sys.exit(1)
# count files to give progress info
iTotal = len(sorted(os.listdir(dirSource)))
# create needed subdirectory
if not os.path.exists(dirDestWeb):
os.makedirs(dirDestWeb)
# move files to subdirs
for index,filename in enumerate( sorted( os.listdir(dirSource) ) ):
m = re.search(r'\.(.*)$', filename )
if m:
ext = m.group(1).lower()
# move CR2 Sources to subfolder
if ext == 'cr2':
# create needed subdirectory
if not os.path.exists(dirDestCR2):
os.makedirs(dirDestCR2)
victim = dirSource + '/' + filename
dest = dirDestCR2 + '/' + filename
shutil.move(victim, dest)
if args.verbose:
print '[' + iNow + '/' + iTotal + ']', 'moved:', filename
# shrink images to websize and move them to subfolder
if ext in ['jpg', 'jpeg', 'png']:
dest = dirSource + '/' + filename
victim = dirDestWeb + '/' + filename
command = "convert -resize %s -quality %d %s %s" % (args.size, args.quality, dest, victim)
(status, output) = commands.getstatusoutput(command)
if args.verbose:
print '[' + str(index + 1) + '/' + str(iTotal) + ']', 'resized:', filename
if __name__ == '__main__':
main()