AkashKumarave commited on
Commit
8e448c7
·
verified ·
1 Parent(s): 7099760

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -27
app.py CHANGED
@@ -8,10 +8,7 @@ import os
8
  app = Flask(__name__)
9
  CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
10
 
11
- def convert_to_vector(image, colormode="color", hierarchical="stacked", mode="spline",
12
- filter_speckle=4, color_precision=6, layer_difference=16,
13
- corner_threshold=60, length_threshold=4.0, max_iterations=10,
14
- splice_threshold=45, path_precision=3):
15
  input_path = "temp_input.jpg"
16
  output_path = "temp_output.svg"
17
 
@@ -19,21 +16,21 @@ def convert_to_vector(image, colormode="color", hierarchical="stacked", mode="sp
19
  # Save the input image to a temporary file
20
  image.save(input_path)
21
 
22
- # Convert the image to SVG using VTracer
23
  vtracer.convert_image_to_svg_py(
24
  input_path,
25
  output_path,
26
- colormode=colormode,
27
- hierarchical=hierarchical,
28
- mode=mode,
29
- filter_speckle=int(filter_speckle),
30
- color_precision=int(color_precision),
31
- layer_difference=int(layer_difference),
32
- corner_threshold=int(corner_threshold),
33
- length_threshold=float(length_threshold),
34
- max_iterations=int(max_iterations),
35
- splice_threshold=int(splice_threshold),
36
- path_precision=int(path_precision)
37
  )
38
 
39
  # Read the SVG output
@@ -51,24 +48,25 @@ def convert_to_vector(image, colormode="color", hierarchical="stacked", mode="sp
51
  @app.route('/convert-to-vector', methods=['POST'])
52
  def convert_to_vector_endpoint():
53
  try:
54
- # Check for raw image data
55
- if not request.data:
 
 
56
  return jsonify({'error': 'No image data provided'}), 400
57
 
58
- # Open image from raw data
59
- image = Image.open(io.BytesIO(request.data)).convert('RGB')
60
-
61
- # Convert to SVG
62
  svg_content = convert_to_vector(image)
63
 
64
- # Return SVG as file
65
- svg_bytes = io.BytesIO(svg_content.encode('utf-8'))
66
- svg_bytes.seek(0)
67
- return send_file(svg_bytes, mimetype='image/svg+xml', as_attachment=True, download_name='vector_output.svg')
 
 
68
  except Exception as e:
69
  return jsonify({'error': str(e)}), 500
70
 
71
- @app.route('/', methods=['GET'])
72
  def index():
73
  return jsonify({'status': 'Image to Vector Converter API is running'})
74
 
 
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.jpg"
13
  output_path = "temp_output.svg"
14
 
 
16
  # Save the input image to a temporary file
17
  image.save(input_path)
18
 
19
+ # Convert the image to SVG using VTracer with default parameters from Gradio app
20
  vtracer.convert_image_to_svg_py(
21
  input_path,
22
  output_path,
23
+ colormode="color",
24
+ hierarchical="stacked",
25
+ mode="spline",
26
+ filter_speckle=4,
27
+ color_precision=6,
28
+ layer_difference=16,
29
+ corner_threshold=60,
30
+ length_threshold=4.0,
31
+ max_iterations=10,
32
+ splice_threshold=45,
33
+ path_precision=3
34
  )
35
 
36
  # Read the SVG output
 
48
  @app.route('/convert-to-vector', methods=['POST'])
49
  def convert_to_vector_endpoint():
50
  try:
51
+ # Handle raw image data in the request body
52
+ if request.data:
53
+ image = Image.open(io.BytesIO(request.data)).convert('RGB')
54
+ else:
55
  return jsonify({'error': 'No image data provided'}), 400
56
 
57
+ # Process the image
 
 
 
58
  svg_content = convert_to_vector(image)
59
 
60
+ # Save the result to a bytes buffer
61
+ svg_io = io.BytesIO(svg_content.encode('utf-8'))
62
+ svg_io.seek(0)
63
+
64
+ # Return the SVG as a response
65
+ return send_file(svg_io, mimetype='image/svg+xml', as_attachment=True, download_name='vector_output.svg')
66
  except Exception as e:
67
  return jsonify({'error': str(e)}), 500
68
 
69
+ @app.route('/')
70
  def index():
71
  return jsonify({'status': 'Image to Vector Converter API is running'})
72