-
Notifications
You must be signed in to change notification settings - Fork 0
/
67-tornado-gallery.py
103 lines (84 loc) · 3.21 KB
/
67-tornado-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Render a local gallery using tornado template:
# https://www.tornadoweb.org/en/stable/
# https://www.tornadoweb.org/en/stable/guide/templates.html
#
# modules installation:
# pip3 install tornado Pillow
#
import tornado.ioloop
import tornado.web
import signal
import sys
import os
from os import listdir
from os.path import isfile, join, exists
from PIL import Image
pictures_dir = '/Users/wenzhili/Pictures' # change to your local Pictures directory before run !!
valid_image_suffix = ('jpg', 'jpeg', 'png')
image_list = None
temp_dir = '.thumbnails'
def prepareImageListAndFolder():
temp_dir_exists = exists(temp_dir)
read_write_mode = 0o755 # the same permission as current user
if not temp_dir_exists:
os.mkdir(temp_dir, read_write_mode)
os.chmod(temp_dir, read_write_mode)
print('>>> thumbnails directory created!')
print('>>> read images from picture directory: {0}'.format(pictures_dir))
onlyfiles = [f for f in listdir(pictures_dir) if isfile(join(pictures_dir, f))]
images = [f for f in onlyfiles if f.endswith(valid_image_suffix)]
return images
def tnails(imageSourceFile, thumbnailFile, size):
try:
image = Image.open(imageSourceFile)
image.thumbnail(size)
image.save(thumbnailFile)
except IOError as error:
print('<<<<<<<<< generate thumbnail error!')
print(error)
def generateThumbnails(images):
if any(os.scandir(temp_dir)):
return print('<<< .thumbnails not empty!')
for img in images:
print('>>> generate: {0}'.format(img))
sourceImgPath = join(pictures_dir, img)
print('>>> full image path: {0}'.format(sourceImgPath))
thumbnailPath = join(temp_dir, img)
tnails(sourceImgPath, thumbnailPath, (120,90))
print('>>>> thumbnails generation done!')
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("<a href='/gallery'>Enter My Gallery</a>")
class GalleryHandler(tornado.web.RequestHandler):
def get(self):
global image_list
items = image_list
self.render("library/tornado_template.html", title="My Gallery", items=items)
async def shutdown():
print('<<< server stopped!')
tornado.ioloop.IOLoop.current().stop()
print('<<< process exited!')
sys.exit(0) # means a clean exit without any errors / problems
def exit_handler(sig, frame):
print('>>> to stop tornado server...')
tornado.ioloop.IOLoop.instance().add_callback_from_signal(shutdown)
if __name__ == "__main__":
# get all the image name list
image_list = prepareImageListAndFolder()
generateThumbnails(image_list)
# start tornado server
server_settings = {
"debug": True,
"autoreload": True
}
application = tornado.web.Application([
(r"/", MainHandler),
(r"/gallery", GalleryHandler),
(r"/thumbnails/(.*)", tornado.web.StaticFileHandler, {"path": "./.thumbnails"}),
(r"/image_raw/(.*)", tornado.web.StaticFileHandler, {"path": pictures_dir}),
], **server_settings)
application.listen(8888)
signal.signal(signal.SIGTERM, exit_handler)
signal.signal(signal.SIGINT, exit_handler)
print('>>> tornado server started!')
tornado.ioloop.IOLoop.current().start()