Skip to content

Commit 80ac2da

Browse files
committed
Added Image folder with image server
1 parent b711bc0 commit 80ac2da

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

Images/server.py

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#!/bin/python
2+
3+
import os
4+
from flask import Flask, Response, request, abort, render_template_string, send_from_directory
5+
import Image
6+
import StringIO
7+
8+
app = Flask(__name__)
9+
10+
WIDTH = 1000
11+
HEIGHT = 800
12+
13+
TEMPLATE = '''
14+
<!DOCTYPE html>
15+
<html>
16+
<head>
17+
<title></title>
18+
<meta charset="utf-8" />
19+
<style>
20+
body {
21+
margin: 0;
22+
background-color: #333;
23+
}
24+
.image {
25+
display: block;
26+
margin: 2em auto;
27+
background-color: #444;
28+
box-shadow: 0 0 10px rgba(0,0,0,0.3);
29+
}
30+
img {
31+
display: block;
32+
}
33+
</style>
34+
<script src="https://code.jquery.com/jquery-1.10.2.min.js" charset="utf-8"></script>
35+
<script src="http://luis-almeida.github.io/unveil/jquery.unveil.min.js" charset="utf-8"></script>
36+
<script>
37+
$(document).ready(function() {
38+
$('img').unveil(1000);
39+
});
40+
</script>
41+
</head>
42+
<body>
43+
{% for image in images %}
44+
<a class="image" href="{{ image.src }}" style="width: {{ image.width }}px; height: {{ image.height }}px">
45+
<img src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==" data-src="{{ image.src }}?w={{ image.width }}&amp;h={{ image.height }}" width="{{ image.width }}" height="{{ image.height }}" />
46+
</a>
47+
{% endfor %}
48+
</body>
49+
'''
50+
51+
@app.route('/<path:filename>')
52+
def image(filename):
53+
try:
54+
w = int(request.args['w'])
55+
h = int(request.args['h'])
56+
except (KeyError, ValueError):
57+
return send_from_directory('.', filename)
58+
59+
try:
60+
im = Image.open(filename)
61+
im.thumbnail((w, h), Image.ANTIALIAS)
62+
io = StringIO.StringIO()
63+
im.save(io, format='JPEG')
64+
return Response(io.getvalue(), mimetype='image/jpeg')
65+
66+
except IOError:
67+
abort(404)
68+
69+
return send_from_directory('.', filename)
70+
71+
@app.route('/')
72+
def index():
73+
images = []
74+
for root, dirs, files in os.walk('.'):
75+
for filename in [os.path.join(root, name) for name in files]:
76+
if not filename.endswith('.jpg'):
77+
continue
78+
im = Image.open(filename)
79+
w, h = im.size
80+
aspect = 1.0*w/h
81+
if aspect > 1.0*WIDTH/HEIGHT:
82+
width = min(w, WIDTH)
83+
height = width/aspect
84+
else:
85+
height = min(h, HEIGHT)
86+
width = height*aspect
87+
images.append({
88+
'width': int(width),
89+
'height': int(height),
90+
'src': filename
91+
})
92+
93+
return render_template_string(TEMPLATE, **{
94+
'images': images
95+
})
96+
97+
if __name__ == '__main__':
98+
app.run(host="0.0.0.0", port=3000, debug=True)

0 commit comments

Comments
 (0)