-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy paththumbs_daemon.py
62 lines (48 loc) · 1.87 KB
/
thumbs_daemon.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
# watches images directory for new files.
# makes thumbnails on demand
from PIL import Image
import os
import time
import sys
allowed_formats = ('JPG', 'JPEG', 'PNG')
def mk_thumbnail(img_dir, filename):
basename, ext = os.path.splitext(filename)
ext = ext[1:].upper() # all my wat at '.png'
if ext.upper() in allowed_formats:
if ext == 'JPG':
ext = 'JPEG'
im = Image.open(os.path.join(img_dir, filename))
im.thumbnail((300, 200))
th_fullpath = os.path.join(img_dir + "thumbs", basename + '.png')
im.save(th_fullpath, ext)
print('Converted ' + filename + ' to thumbnail.', end='\r')
else:
print('Failed to build thumbnail: unknown file extension ' + ext,
file=sys.stderr
)
if __name__ == '__main__':
img_dir = '/home/dap/projects/ridiculously.moe/img/'
while True:
found_new = None
for img_filename in os.listdir(img_dir):
img_base, img_ext = os.path.splitext(img_filename)
if img_ext.upper() not in allowed_formats:
continue # that's not an image!
if not os.path.isfile(os.path.join(img_dir,
'thumbs',
img_base + '.png')):
# we don't have a thumbnail for this one.
try:
mk_thumbnail(img_dir, img_filename)
found_new = True
except OSError as e:
# sometimes we're too fast, so the file isn't done
# being written.
if "not processed" in str(e):
pass # >.> get it on the next pass.
else:
raise OSError(e)
if found_new:
print('--')
found_new = None
time.sleep(1)