-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtrace.py
71 lines (53 loc) · 1.72 KB
/
trace.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
import os
import shutil
import requests
import base64
from flask import Flask
from flask import request
from flask import abort
from flask import send_file
from subprocess import call
app = Flask(__name__)
@app.route('/', methods=['POST'])
def image_from_body():
cont = request.json
if('url' in cont):
data = cont['url']
return trace(data, False)
if('b64' in cont):
data = cont['b64']
data = data.split(',')[1]
return trace(data, True)
@app.route('/', methods=['GET'])
def image_from_url():
return trace(request.args.get('url', type = str), False)
def trace(image_requested, is_base64):
input_image_file = '/tmp/input'
bmp_image_file = '/tmp/temp.bmp'
svg_filename = '/tmp/trace.svg'
if not is_base64:
url = image_requested
if not url:
return "please provide URL of image to trace with ?url="
# Download image from URL
response = requests.get(url, stream=True)
with open(input_image_file, 'wb') as file:
shutil.copyfileobj(response.raw, file)
del response
else:
imgdata = base64.b64decode(image_requested)
with open(input_image_file, 'wb') as f:
f.write(imgdata)
# Convert image to .bmp
call('convert %s %s' % (input_image_file, bmp_image_file), shell=True)
# Trace .bmp to .svg
call('potrace %s -o %s --svg' % (bmp_image_file, svg_filename), shell=True)
return send_file(svg_filename, mimetype='image/svg+xml')
@app.after_request
def cleanup(response):
location = '/tmp/traces'
if os.path.isdir(location):
shutil.rmtree(location)
return response
if __name__ == "__main__":
app.run(host='0.0.0.0',port=8080)