AkashKumarave commited on
Commit
f63ea57
·
verified ·
1 Parent(s): da0511b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -73
app.py CHANGED
@@ -1,81 +1,67 @@
1
-
2
- import gradio as gr
3
- import vtracer
4
- import os
5
- import base64
6
- from io import BytesIO
7
- from PIL import Image
8
- import json
9
  from flask import Flask, request, jsonify
10
  from flask_cors import CORS
 
 
 
 
11
 
12
  app = Flask(__name__)
13
- CORS(app)
 
 
 
 
 
 
 
14
 
15
- @app.route('/api/predict', methods=['POST'])
16
- def predict():
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  try:
18
- # Get image file from request
19
- if 'image' not in request.files:
20
- return jsonify({"error": "No image file provided"}), 400
21
-
22
- image_file = request.files['image']
23
- image = Image.open(image_file)
24
-
25
- # Get parameters from request
26
- colormode = request.form.get('colormode', 'color')
27
- hierarchical = request.form.get('hierarchical', 'stacked')
28
- mode = request.form.get('mode', 'spline')
29
- filter_speckle = int(request.form.get('filter_speckle', 4))
30
- color_precision = int(request.form.get('color_precision', 6))
31
- layer_difference = int(request.form.get('layer_difference', 16))
32
- corner_threshold = int(request.form.get('corner_threshold', 60))
33
- length_threshold = float(request.form.get('length_threshold', 4.0))
34
- max_iterations = int(request.form.get('max_iterations', 10))
35
- splice_threshold = int(request.form.get('splice_threshold', 45))
36
- path_precision = int(request.form.get('path_precision', 3))
37
-
38
- # Create temp directory if it doesn't exist
39
- os.makedirs('temp', exist_ok=True)
40
-
41
- # Save the input image to a temporary file
42
- input_path = "temp/temp_input.jpg"
43
- output_path = "temp/svg_output.svg"
44
-
45
- image.save(input_path)
46
-
47
- # Convert the image to SVG using VTracer
48
- vtracer.convert_image_to_svg_py(
49
- input_path,
50
- output_path,
51
- colormode=colormode,
52
- hierarchical=hierarchical,
53
- mode=mode,
54
- filter_speckle=filter_speckle,
55
- color_precision=color_precision,
56
- layer_difference=layer_difference,
57
- corner_threshold=corner_threshold,
58
- length_threshold=length_threshold,
59
- max_iterations=max_iterations,
60
- splice_threshold=splice_threshold,
61
- path_precision=path_precision
62
- )
63
-
64
- # Read the SVG output
65
- with open(output_path, "r") as f:
66
- svg_content = f.read()
67
-
68
- # Create SVG with viewBox
69
- full_svg = f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>'
70
-
71
- # Return SVG content and path
72
- return jsonify({
73
- "data": [full_svg, output_path],
74
- "success": True
75
- })
76
-
77
  except Exception as e:
78
- return jsonify({"error": str(e), "success": False}), 500
 
 
 
 
79
 
80
- if __name__ == "__main__":
81
- app.run(host='0.0.0.0', port=7860)
 
 
 
 
 
 
 
 
 
1
  from flask import Flask, request, jsonify
2
  from flask_cors import CORS
3
+ from PIL import Image
4
+ import vtracer
5
+ import io
6
+ import os
7
 
8
  app = Flask(__name__)
9
+ CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
10
+
11
+ def convert_to_vector(image):
12
+ input_path = "temp_input.png"
13
+ output_path = "svg_output.svg"
14
+
15
+ # Save input image
16
+ image.save(input_path)
17
 
18
+ # Convert to SVG using VTracer
19
+ vtracer.convert_image_to_svg_py(
20
+ input_path,
21
+ output_path,
22
+ colormode="color",
23
+ hierarchical="stacked",
24
+ mode="spline",
25
+ filter_speckle=4,
26
+ color_precision=6,
27
+ layer_difference=16,
28
+ corner_threshold=60,
29
+ length_threshold=4.0,
30
+ max_iterations=10,
31
+ splice_threshold=45,
32
+ path_precision=3
33
+ )
34
+
35
+ # Read SVG content
36
+ with open(output_path, "r") as f:
37
+ svg_content = f.read()
38
+
39
+ # Clean up
40
+ os.remove(input_path)
41
+ os.remove(output_path)
42
+
43
+ return svg_content
44
+
45
+ @app.route('/convert-to-vector', methods=['POST'])
46
+ def convert_to_vector_endpoint():
47
  try:
48
+ if not request.data:
49
+ return jsonify({'error': 'No image data provided'}), 400
50
+
51
+ # Open image
52
+ image = Image.open(io.BytesIO(request.data)).convert('RGB')
53
+
54
+ # Convert to SVG
55
+ svg_content = convert_to_vector(image)
56
+
57
+ return svg_content, 200, {'Content-Type': 'text/plain'}
58
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59
  except Exception as e:
60
+ return jsonify({'error': str(e)}), 500
61
+
62
+ @app.route('/')
63
+ def index():
64
+ return jsonify({'status': 'Image to Vector API is running'})
65
 
66
+ if __name__ == '__main__':
67
+ app.run(host='0.0.0.0', port=7860)