AkashKumarave commited on
Commit
470bbd3
·
verified ·
1 Parent(s): 9c3d6e6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +19 -13
app.py CHANGED
@@ -4,13 +4,15 @@ 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.jpg"
13
- output_path = "temp_output.svg"
 
14
 
15
  # Save the input image to a temporary file
16
  image.save(input_path)
@@ -18,7 +20,7 @@ def convert_to_vector(image):
18
  # Convert the image to SVG using VTracer with default parameters
19
  vtracer.convert_image_to_svg_py(
20
  input_path,
21
- output_path,
22
  colormode="color",
23
  hierarchical="stacked",
24
  mode="spline",
@@ -32,15 +34,19 @@ def convert_to_vector(image):
32
  path_precision=3
33
  )
34
 
35
- # Read the SVG output
36
- with open(output_path, "rb") as f:
37
- svg_content = f.read()
 
 
 
38
 
39
  # Clean up temporary files
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():
@@ -48,7 +54,7 @@ def convert_to_vector_endpoint():
48
  # Handle raw image data in the request body
49
  if request.data:
50
  image = Image.open(io.BytesIO(request.data)).convert('RGB')
51
- # Fallback for multipart/form-data (if needed later)
52
  elif 'image' in request.files:
53
  file = request.files['image']
54
  image = Image.open(file).convert('RGB')
@@ -56,14 +62,14 @@ 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),
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
 
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": "*"}})
11
 
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)
 
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,
24
  colormode="color",
25
  hierarchical="stacked",
26
  mode="spline",
 
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():
 
54
  # Handle raw image data in the request body
55
  if request.data:
56
  image = Image.open(io.BytesIO(request.data)).convert('RGB')
57
+ # Fallback for multipart/form-data
58
  elif 'image' in request.files:
59
  file = request.files['image']
60
  image = Image.open(file).convert('RGB')
 
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