-
Notifications
You must be signed in to change notification settings - Fork 0
/
webGallery.py
89 lines (73 loc) · 2.31 KB
/
webGallery.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
import os
import web
import json
import sequenceGen
import imageio
import threading
paths = []
cType = {
"png":"images/png",
"jpg":"images/jpeg",
"gif":"images/gif",
}
imagePath = 'images'
_fps = 10
class Gallery:
def GET(self):
global paths
paths = getImagePaths()
web.header('Content-Type', 'text/html')
render = web.template.render('gallery/')
jsonPaths = json.dumps(paths)
print(jsonPaths)
return render.gallery(paths)
class GetImage:
def GET(self, path):
name = 'compiled.gif'
ext = name.split(".")[-1] # Gather extension
if(os.path.exists('images/' + path + '/compiled.gif')):
print('Serving existing gif')
else:
print('Gif not found. Compiling jpgs')
gif = createGifFromPath('images/' + path)
if(gif == False):
raise web.notfound()
web.header("Content-Type", cType[ext])
return gif
print('nam:' + name)
print('images/' + path + '/' + name)
if name in os.listdir('images/' + path): # Security
web.header("Content-Type", cType[ext]) # Set the Header
return open('images/' + path + '/' + name, "rb").read() # Notice 'rb' for reading images
else:
raise web.notfound()
def createGifFromPath(path):
numpyBuffer = []
print('input path: ' + path)
try:
# for image in os.listdir(path):
zigZag = sequenceGen.zigZag(len(numpyBuffer))
for image in os.listdir(path):
ext = image.split(".")[-1] # Gather extension
if(ext == 'jpg'):
imageRelPath = path + '/' + image
print('appending image path: ' + imageRelPath)
numpyBuffer.append(imageio.imread(imageRelPath))
gif = imageio.mimwrite(imageio.RETURN_BYTES, numpyBuffer, format='gif', fps=_fps)
name = path + '/compiled.gif'
threading.Thread(target=saveImage, args=[gif, name]).start() # save image in thread.
return gif # and serve the buffer to client in the mean time.
except Exception as e:
print('could not compile gifs Error: ' + str(e))
return False
def saveImage(gif, name):
with open(name, 'wb') as f:
f.write(gif)
print(name + ': image saved!')
def getImagePaths():
gifPaths = []
for filename in os.listdir(imagePath):
currentPath = filename + '/'
if(os.path.exists('images/' + currentPath + '/0.jpg') or os.path.exists('images/' + currentPath + '/compiled.gif')): #only serve images where at least 1 image is present.
gifPaths.append(currentPath)
return sorted(gifPaths)