AkashKumarave commited on
Commit
083e606
·
verified ·
1 Parent(s): c56d283

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -16
app.py CHANGED
@@ -4,7 +4,6 @@ from PIL import Image
4
  import vtracer
5
  import io
6
  import os
7
- import cairosvg
8
 
9
  app = Flask(__name__)
10
  CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
@@ -12,12 +11,11 @@ CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
12
  def convert_to_vector(image):
13
  input_path = "temp_input.jpg"
14
  output_svg_path = "temp_output.svg"
15
- output_png_path = "temp_output.png"
16
 
17
  # Save the input image to a temporary file
18
  image.save(input_path)
19
 
20
- # Convert the image to SVG using VTracer with default parameters
21
  vtracer.convert_image_to_svg_py(
22
  input_path,
23
  output_svg_path,
@@ -34,19 +32,15 @@ def convert_to_vector(image):
34
  path_precision=3
35
  )
36
 
37
- # Convert SVG to PNG
38
- cairosvg.svg2png(url=output_svg_path, write_to=output_png_path)
39
-
40
- # Read the PNG output
41
- with open(output_png_path, "rb") as f:
42
- png_content = f.read()
43
 
44
  # Clean up temporary files
45
  os.remove(input_path)
46
  os.remove(output_svg_path)
47
- os.remove(output_png_path)
48
 
49
- return png_content
50
 
51
  @app.route('/convert-to-vector', methods=['POST'])
52
  def convert_to_vector_endpoint():
@@ -62,14 +56,14 @@ def convert_to_vector_endpoint():
62
  return jsonify({'error': 'No image data provided'}), 400
63
 
64
  # Process the image
65
- png_content = convert_to_vector(image)
66
 
67
- # Return the PNG as a response
68
  return send_file(
69
- io.BytesIO(png_content),
70
- mimetype='image/png',
71
  as_attachment=True,
72
- download_name='vector_output.png'
73
  )
74
  except Exception as e:
75
  return jsonify({'error': str(e)}), 500
 
4
  import vtracer
5
  import io
6
  import os
 
7
 
8
  app = Flask(__name__)
9
  CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
 
11
  def convert_to_vector(image):
12
  input_path = "temp_input.jpg"
13
  output_svg_path = "temp_output.svg"
 
14
 
15
  # Save the input image to a temporary file
16
  image.save(input_path)
17
 
18
+ # Convert the image to SVG using VTracer
19
  vtracer.convert_image_to_svg_py(
20
  input_path,
21
  output_svg_path,
 
32
  path_precision=3
33
  )
34
 
35
+ # Read the SVG output
36
+ with open(output_svg_path, "r") as f:
37
+ svg_content = f.read()
 
 
 
38
 
39
  # Clean up temporary files
40
  os.remove(input_path)
41
  os.remove(output_svg_path)
 
42
 
43
+ return svg_content
44
 
45
  @app.route('/convert-to-vector', methods=['POST'])
46
  def convert_to_vector_endpoint():
 
56
  return jsonify({'error': 'No image data provided'}), 400
57
 
58
  # Process the image
59
+ svg_content = convert_to_vector(image)
60
 
61
+ # Return the SVG as a response
62
  return send_file(
63
+ io.BytesIO(svg_content.encode('utf-8')),
64
+ mimetype='image/svg+xml',
65
  as_attachment=True,
66
+ download_name='vector_output.svg'
67
  )
68
  except Exception as e:
69
  return jsonify({'error': str(e)}), 500