-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathresize.py
More file actions
39 lines (30 loc) · 1.06 KB
/
Copy pathresize.py
File metadata and controls
39 lines (30 loc) · 1.06 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
#!/usr/bin/env python
import fnmatch
import os
from PIL import Image
size = 512, 512
def all_files(root, patterns='*', single_level=False, yield_folders=False):
patterns = patterns.split(';')
for path, subdirs, files in os.walk(root):
if yield_folders:
files.extend(subdirs)
files.sort()
for name in files:
for pattern in patterns:
if fnmatch.fnmatch(name, pattern):
yield os.path.join(path, name)
break
if single_level:
break
def resize(picture_path):
for path in all_files(os.path.expanduser(picture_path), '*.jpg'):
filename = os.path.splitext(path)[0].rsplit('/', 1)[-1]
outfile = filename + ".thumbnail.jpg"
try:
im = Image.open(path)
im.thumbnail(size, Image.ANTIALIAS)
im.save('resize/{0}'.format(outfile), "JPEG")
except IOError:
print "cannot create thumbnail for '%s'" % outfile
if __name__ == "__main__":
resize('~/Pictures/Nokia')