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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -25
app.py CHANGED
@@ -4,61 +4,83 @@ 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_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,
22
- colormode="color",
23
- hierarchical="stacked",
24
- mode="spline",
25
- filter_speckle=4,
26
- color_precision=6,
27
- layer_difference=16,
28
- corner_threshold=60,
29
- length_threshold=4.0,
30
- max_iterations=10,
31
- splice_threshold=45,
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():
47
  try:
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
52
  elif 'image' in request.files:
 
53
  file = request.files['image']
54
  image = Image.open(file).convert('RGB')
55
  else:
 
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',
@@ -66,6 +88,7 @@ def convert_to_vector_endpoint():
66
  download_name='vector_output.svg'
67
  )
68
  except Exception as e:
 
69
  return jsonify({'error': str(e)}), 500
70
 
71
  @app.route('/')
 
4
  import vtracer
5
  import io
6
  import os
7
+ import logging
8
 
9
  app = Flask(__name__)
10
  CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
11
 
12
+ # Configure logging
13
+ logging.basicConfig(level=logging.DEBUG)
14
+
15
  def convert_to_vector(image):
16
  input_path = "temp_input.jpg"
17
  output_svg_path = "temp_output.svg"
18
 
19
+ try:
20
+ # Save the input image to a temporary file
21
+ app.logger.debug("Saving input image to %s", input_path)
22
+ image.save(input_path)
23
+
24
+ # Convert the image to SVG using VTracer
25
+ app.logger.debug("Converting image to SVG")
26
+ vtracer.convert_image_to_svg_py(
27
+ input_path,
28
+ output_svg_path,
29
+ colormode="color",
30
+ hierarchical="stacked",
31
+ mode="spline",
32
+ filter_speckle=4,
33
+ color_precision=6,
34
+ layer_difference=16,
35
+ corner_threshold=60,
36
+ length_threshold=4.0,
37
+ max_iterations=10,
38
+ splice_threshold=45,
39
+ path_precision=3
40
+ )
41
 
42
+ # Read the SVG output
43
+ app.logger.debug("Reading SVG from %s", output_svg_path)
44
+ with open(output_svg_path, "r") as f:
45
+ svg_content = f.read()
 
 
 
 
 
 
 
 
 
 
 
 
46
 
47
+ # Validate SVG content
48
+ if not svg_content.startswith('<?xml') or '<svg' not in svg_content:
49
+ app.logger.error("Invalid SVG content: %s", svg_content[:100])
50
+ raise ValueError("Generated SVG is invalid")
51
 
52
+ return svg_content
 
 
53
 
54
+ finally:
55
+ # Clean up temporary files
56
+ app.logger.debug("Cleaning up temporary files")
57
+ if os.path.exists(input_path):
58
+ os.remove(input_path)
59
+ if os.path.exists(output_svg_path):
60
+ os.remove(output_svg_path)
61
 
62
  @app.route('/convert-to-vector', methods=['POST'])
63
  def convert_to_vector_endpoint():
64
  try:
65
  # Handle raw image data in the request body
66
  if request.data:
67
+ app.logger.debug("Received raw image data")
68
  image = Image.open(io.BytesIO(request.data)).convert('RGB')
69
  # Fallback for multipart/form-data
70
  elif 'image' in request.files:
71
+ app.logger.debug("Received multipart image data")
72
  file = request.files['image']
73
  image = Image.open(file).convert('RGB')
74
  else:
75
+ app.logger.error("No image data provided")
76
  return jsonify({'error': 'No image data provided'}), 400
77
 
78
  # Process the image
79
+ app.logger.debug("Processing image")
80
  svg_content = convert_to_vector(image)
81
 
82
  # Return the SVG as a response
83
+ app.logger.debug("Sending SVG response")
84
  return send_file(
85
  io.BytesIO(svg_content.encode('utf-8')),
86
  mimetype='image/svg+xml',
 
88
  download_name='vector_output.svg'
89
  )
90
  except Exception as e:
91
+ app.logger.error("Error processing request: %s", str(e))
92
  return jsonify({'error': str(e)}), 500
93
 
94
  @app.route('/')