AkashKumarave commited on
Commit
3f63654
·
verified ·
1 Parent(s): 192f53a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -31
app.py CHANGED
@@ -4,72 +4,83 @@ 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": "*"}})
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)
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,
24
- colormode="color",
25
- hierarchical="stacked",
26
- mode="spline",
27
- filter_speckle=4,
28
- color_precision=6,
29
- layer_difference=16,
30
- corner_threshold=60,
31
- length_threshold=4.0,
32
- max_iterations=10,
33
- splice_threshold=45,
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():
53
  try:
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')
61
  else:
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": "*"}})
10
 
11
+ def convert_to_vector(image, params):
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 with provided parameters
19
  vtracer.convert_image_to_svg_py(
20
  input_path,
21
  output_svg_path,
22
+ colormode=params.get("colormode", "color"),
23
+ hierarchical=params.get("hierarchical", "stacked"),
24
+ mode=params.get("mode", "spline"),
25
+ filter_speckle=int(params.get("filter_speckle", 4)),
26
+ color_precision=int(params.get("color_precision", 6)),
27
+ layer_difference=int(params.get("layer_difference", 16)),
28
+ corner_threshold=int(params.get("corner_threshold", 60)),
29
+ length_threshold=float(params.get("length_threshold", 4.0)),
30
+ max_iterations=int(params.get("max_iterations", 10)),
31
+ splice_threshold=int(params.get("splice_threshold", 45)),
32
+ path_precision=int(params.get("path_precision", 3))
33
  )
34
 
35
+ # Read the SVG output
36
+ with open(output_svg_path, "rb") 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():
47
  try:
48
+ # Handle image data
49
+ if request.data and not request.is_json:
50
  image = Image.open(io.BytesIO(request.data)).convert('RGB')
 
51
  elif 'image' in request.files:
52
  file = request.files['image']
53
  image = Image.open(file).convert('RGB')
54
  else:
55
  return jsonify({'error': 'No image data provided'}), 400
56
 
57
+ # Get parameters from JSON payload or query parameters
58
+ if request.is_json:
59
+ params = request.get_json()
60
+ else:
61
+ params = {
62
+ "colormode": request.args.get("colormode", "color"),
63
+ "hierarchical": request.args.get("hierarchical", "stacked"),
64
+ "mode": request.args.get("mode", "spline"),
65
+ "filter_speckle": request.args.get("filter_speckle", 4),
66
+ "color_precision": request.args.get("color_precision", 6),
67
+ "layer_difference": request.args.get("layer_difference", 16),
68
+ "corner_threshold": request.args.get("corner_threshold", 60),
69
+ "length_threshold": request.args.get("length_threshold", 4.0),
70
+ "max_iterations": request.args.get("max_iterations", 10),
71
+ "splice_threshold": request.args.get("splice_threshold", 45),
72
+ "path_precision": request.args.get("path_precision", 3)
73
+ }
74
+
75
  # Process the image
76
+ svg_content = convert_to_vector(image, params)
77
 
78
+ # Return the SVG as a response
79
  return send_file(
80
+ io.BytesIO(svg_content),
81
+ mimetype='image/svg+xml',
82
  as_attachment=True,
83
+ download_name='vector_output.svg'
84
  )
85
  except Exception as e:
86
  return jsonify({'error': str(e)}), 500