-
Notifications
You must be signed in to change notification settings - Fork 2
/
resizephotos.py
executable file
·94 lines (79 loc) · 2.63 KB
/
resizephotos.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env python
import os,sys,re
def get_files(dir,filetypes):
tmp = os.listdir(dir)
retfiles = []
for x in tmp:
for y in filetypes:
if re.search(y+'$',x,re.IGNORECASE):
if os.path.isfile(dir+'/'+x):
retfiles.append(x)
return retfiles
parameters = {}
validoptions = ['-d','-r','-e','-t']
if len(sys.argv) == 2:
if sys.argv[1] == '--help':
print """Available options:
-d <dir>: Convert files in <dir>. Default is current working dir.
-t jpeg,jpg: Types of files to convert (case insensative). Default is jpeg,jpg.
-e <path of convert binary>: Absolute path of convert binary from ImageMagik package. Default /usr/local/bin/convert
-r <resize>: Resize to new size. Default is 640x480.
"""
print """Note:
<resize> directory is created in <dir> and all files are stored in that."""
sys.exit(0)
else:
print "Invalid parameter passed. Try '--help'."
sys.exit(1)
if not len(sys.argv)%2:
print "invalid number of parameters"
sys.exit(1)
for x in range(1,len(sys.argv),2):
if not sys.argv[x].startswith('-'):
print "Invalid option passed:",sys.argv[x]
sys.exit(1)
parameters[sys.argv[x]] = sys.argv[x+1]
for x in parameters.keys():
if x not in validoptions:
print x,"is not valid option"
sys.exit(1)
if '-d' not in parameters.keys():
parameters['-d'] = os.getcwd()
if '-r' not in parameters.keys():
parameters['-r'] = '640x480'
if '-e' not in parameters.keys():
parameters['-e'] = '/usr/local/bin/convert'
if '-t' not in parameters.keys():
parameters['-t'] = 'jpeg,jpg';
if not os.path.isfile(parameters['-e']):
print parameters['-e'],"is not a valid file or it does not exits"
sys.exit(1)
if not os.path.isdir(parameters['-d']):
print parameters['-d'],"does not exists"
sys.exit(1)
if len(parameters['-r'].split('x')) != 2:
print parameters['-r'],"is not valid. Enter XxY where X,Y are numeric"
sys.exit(1)
resize = {}
resize['x'],resize['y'] = parameters['-r'].split('x')
for x in resize.keys():
if not re.search('^\d+$',resize[x]):
print resize[x],"is not a numeric value"
sys.exit(1)
if not os.path.isdir(parameters['-d']+'/'+parameters['-r']):
try:
os.mkdir(parameters['-d']+'/'+parameters['-r'])
except:
print "Error creating dir",parameters['-d']+'/'+parameters['-r']
sys.exit(1)
files = get_files(parameters['-d'],parameters['-t'].split(','))
if not len(files):
print "Cannot find any files for resizing."
sys.exit(2)
for x in files:
(stdin,stdout,stderr) = os.popen3(parameters['-e']+" -resize "+parameters['-r']+' "'+parameters['-d']+'/'+x+'" "'+parameters['-d']+'/'+parameters['-r']+'/'+x+'"')
y = stderr.read()
if y:
print y
else:
print "Successfully converted",parameters['-d']+'/'+x