-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhandler.py
More file actions
executable file
·79 lines (71 loc) · 2.88 KB
/
Copy pathhandler.py
File metadata and controls
executable file
·79 lines (71 loc) · 2.88 KB
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
from mod_python import apache, util
import convert as converter
from convert import shapes
from os import path
import re
def index(req):
shape_choices = ''.join(['''<option value="%s">%s</option>''' % (shape, shape) for shape in shapes])
return '''
<html>
<head>
<title>Derasterizer: Raster image to SVG converter</title>
</head>
<body>
<h1>Bitmap to SVG converter</h1>
<p>Upload a bitmap file (.jpg, .png, .gif) to convert it to a SVG image.</p>
<p></p>
<form action="/handler.py/convert" method="post" enctype="multipart/form-data">
Upload a file: <input name="file" id="file" type="file"><br/>
Block size (8): <input name="block_size" type="number" min="2" max="256" placeholder="8"><br/>
Alpha adjustment (0.0 - 2.0): <input name="alpha_value" placeholder="1.0"><br/>
Filter limit (0.0 - 1.0): <input name="filter_limit" placeholder="0.1"><br/>
Shape type: <select name="shape">%s</select><br/>
<input type="submit">
</form>
</body>
</html>
''' % shape_choices
def convert(req):
tmpfile = req.form["file"]
block_size = int(req.form.get("block_size", None) or 8)
alpha_value = float(req.form.get("alpha_value", None) or 1.0)
filter_limit = float(req.form.get("filter_limit", None) or (block_size / 5.0))
shape = req.form.get("shape", None)
leafname, ext = path.splitext(tmpfile.filename)
newfile = converter.convert(tmpfile.file,
shape_name=shape,
block_size=block_size,
alpha_value=alpha_value,
filter_limit=filter_limit,
outfile="/var/www/images/%s.svg" % leafname)
basename = path.basename(newfile)
util.redirect(req, "/handler.py/display?img=%s" % basename)
def display(req, img=None):
return '''
<html>
<head>
<title>Derasterizer: Preview</title>
</head>
<body>
<img style="height:97%%; width:98%%" src="/images/%(img)s"><br/>
<a href="/">Back</a>
<a href="/images/%(img)s">Direct link (right-click to save)</a>
<a href="/handler.py/fullsize?img=%(img)s">View full size</a>
</body>
</html>
''' % {'img': img}
def fullsize(req, img=None):
svgfile = open("/var/www/images/%s" % img)
results = re.search(r'viewBox="0 0 (\d+) (\d+)"', svgfile.read(500))
if results:
width, height = results.groups()
return '''
<html>
<head>
<title>Derasterizer: Full sized</title>
</head>
<body>
<div style="height: %spx; width: %spx; background-image: url('/images/%s');"/>
</body>
</html>
''' % (height, width, img)