AkashKumarave commited on
Commit
5320d53
·
verified ·
1 Parent(s): ab2459d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -37
app.py CHANGED
@@ -4,75 +4,72 @@ 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, params):
12
  input_path = "temp_input.jpg"
13
  output_svg_path = "temp_output.svg"
 
14
 
 
15
  image.save(input_path)
16
 
 
17
  vtracer.convert_image_to_svg_py(
18
  input_path,
19
  output_svg_path,
20
- colormode=params.get("colormode", "color"),
21
- hierarchical=params.get("hierarchical", "stacked"),
22
- mode=params.get("mode", "spline"),
23
- filter_speckle=int(params.get("filter_speckle", 4)),
24
- color_precision=int(params.get("color_precision", 6)),
25
- layer_difference=int(params.get("layer_difference", 16)),
26
- corner_threshold=int(params.get("corner_threshold", 60)),
27
- length_threshold=float(params.get("length_threshold", 4.0)),
28
- max_iterations=int(params.get("max_iterations", 10)),
29
- splice_threshold=int(params.get("splice_threshold", 45)),
30
- path_precision=int(params.get("path_precision", 3))
31
  )
32
 
33
- with open(output_svg_path, "rb") as f:
34
- svg_content = f.read()
35
 
 
 
 
 
 
36
  os.remove(input_path)
37
  os.remove(output_svg_path)
 
38
 
39
- return svg_content
40
 
41
  @app.route('/convert-to-vector', methods=['POST'])
42
  def convert_to_vector_endpoint():
43
  try:
44
- if request.data and not request.is_json:
 
45
  image = Image.open(io.BytesIO(request.data)).convert('RGB')
 
46
  elif 'image' in request.files:
47
  file = request.files['image']
48
  image = Image.open(file).convert('RGB')
49
  else:
50
  return jsonify({'error': 'No image data provided'}), 400
51
 
52
- if request.is_json:
53
- params = request.get_json()
54
- else:
55
- params = {
56
- "colormode": request.args.get("colormode", "color"),
57
- "hierarchical": request.args.get("hierarchical", "stacked"),
58
- "mode": request.args.get("mode", "spline"),
59
- "filter_speckle": request.args.get("filter_speckle", 4),
60
- "color_precision": request.args.get("color_precision", 6),
61
- "layer_difference": request.args.get("layer_difference", 16),
62
- "corner_threshold": request.args.get("corner_threshold", 60),
63
- "length_threshold": request.args.get("length_threshold", 4.0),
64
- "max_iterations": request.args.get("max_iterations", 10),
65
- "splice_threshold": request.args.get("splice_threshold", 45),
66
- "path_precision": request.args.get("path_precision", 3)
67
- }
68
-
69
- svg_content = convert_to_vector(image, params)
70
 
 
71
  return send_file(
72
- io.BytesIO(svg_content),
73
- mimetype='image/svg+xml',
74
  as_attachment=True,
75
- download_name='vector_output.svg'
76
  )
77
  except Exception as e:
78
  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)
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