AkashKumarave commited on
Commit
7099760
·
verified ·
1 Parent(s): 27433cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -36
app.py CHANGED
@@ -15,59 +15,60 @@ def convert_to_vector(image, colormode="color", hierarchical="stacked", mode="sp
15
  input_path = "temp_input.jpg"
16
  output_path = "temp_output.svg"
17
 
18
- # Save the input image to a temporary file
19
- image.save(input_path)
20
-
21
- # Convert the image to SVG using VTracer
22
- vtracer.convert_image_to_svg_py(
23
- input_path,
24
- output_path,
25
- colormode=colormode,
26
- hierarchical=hierarchical,
27
- mode=mode,
28
- filter_speckle=int(filter_speckle),
29
- color_precision=int(color_precision),
30
- layer_difference=int(layer_difference),
31
- corner_threshold=int(corner_threshold),
32
- length_threshold=float(length_threshold),
33
- max_iterations=int(max_iterations),
34
- splice_threshold=int(splice_threshold),
35
- path_precision=int(path_precision)
36
- )
37
 
38
- # Read the SVG output
39
- with open(output_path, "r") as f:
40
- svg_content = f.read()
 
 
 
 
 
 
 
 
 
 
 
 
 
41
 
42
- # Clean up temporary files
43
- os.remove(input_path)
44
- os.remove(output_path)
45
 
46
- return svg_content
 
 
 
 
 
 
47
 
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
- # Convert image to SVG with default parameters (can be extended with query params if needed)
 
 
 
58
  svg_content = convert_to_vector(image)
59
 
60
- # Save SVG content to a bytes buffer
61
- svg_io = io.StringIO(svg_content)
62
  svg_bytes = io.BytesIO(svg_content.encode('utf-8'))
63
  svg_bytes.seek(0)
64
-
65
- # Return the SVG as a file response
66
  return send_file(svg_bytes, mimetype='image/svg+xml', as_attachment=True, download_name='vector_output.svg')
67
  except Exception as e:
68
  return jsonify({'error': str(e)}), 500
69
 
70
- @app.route('/')
71
  def index():
72
  return jsonify({'status': 'Image to Vector Converter API is running'})
73
 
 
15
  input_path = "temp_input.jpg"
16
  output_path = "temp_output.svg"
17
 
18
+ try:
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
40
+ with open(output_path, "r") as f:
41
+ svg_content = f.read()
42
 
43
+ return svg_content
44
+ finally:
45
+ # Clean up temporary files
46
+ if os.path.exists(input_path):
47
+ os.remove(input_path)
48
+ if os.path.exists(output_path):
49
+ os.remove(output_path)
50
 
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