-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
68 lines (55 loc) · 1.86 KB
/
main.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
import os
from flask import Flask, Response, request, abort, render_template, send_from_directory, send_file
from io import BytesIO
from PIL import Image
app = Flask(__name__)
WIDTH = 700
HEIGHT = 600
@app.route('/cards/<path:filename>')
def image(filename):
try:
w = int(request.args['w'])
h = int(request.args['h'])
except (KeyError, ValueError):
print(os.path.dirname(__file__) + '/cards/'+ filename)
return send_from_directory(os.path.dirname(__file__) + '/cards', filename)
try:
path = os.path.join(os.path.dirname(__file__) + '/cards', filename)
im = Image.open(path)
im.thumbnail((w, h), Image.ANTIALIAS)
io = BytesIO()
im.save(io, format='PNG')
io.seek(0)
return send_file(io, mimetype='image/png')
except IOError as e:
print(e)
abort(404)
return send_from_directory(os.path.dirname(__file__) + '/cards', filename)
@app.route('/')
def index():
images = []
for root, dirs, files in os.walk(os.path.dirname(__file__) + '/cards/'):
files.sort(reverse=True)
for filename in [os.path.join(root, name) for name in files]:
if not filename.endswith('.jpg'):
continue
im = Image.open(filename+"-upper.png")
name = filename.split("/")
w, h = im.size
aspect = 1.0*w/h
if aspect > 1.0*WIDTH/HEIGHT:
width = min(w, WIDTH)
height = width/aspect
else:
height = min(h, HEIGHT)
width = height*aspect
images.append({
'width': int(width),
'height': int(height),
'src': './cards/'+str(name[-1])
})
return render_template('index.html', **{
'images': images
})
if __name__ == '__main__':
app.run()