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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -18
app.py CHANGED
@@ -10,18 +10,18 @@ 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,
@@ -39,34 +39,33 @@ def convert_to_vector(image):
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']
@@ -75,11 +74,11 @@ def convert_to_vector_endpoint():
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')),
@@ -88,7 +87,7 @@ def convert_to_vector_endpoint():
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('/')
 
10
  CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
11
 
12
  # Configure logging
13
+ logging.basicConfig(level=logging.DEBUG, format='%(asctime)s - %(levelname)s - %(message)s')
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
21
+ app.logger.debug(f"Saving input image to {input_path}")
22
  image.save(input_path)
23
 
24
+ # Convert to SVG using VTracer
25
  app.logger.debug("Converting image to SVG")
26
  vtracer.convert_image_to_svg_py(
27
  input_path,
 
39
  path_precision=3
40
  )
41
 
42
+ # Read the SVG
43
+ app.logger.debug(f"Reading SVG from {output_svg_path}")
44
  with open(output_svg_path, "r") as f:
45
  svg_content = f.read()
46
 
47
+ # Validate SVG
48
  if not svg_content.startswith('<?xml') or '<svg' not in svg_content:
49
+ app.logger.error(f"Invalid SVG content: {svg_content[:100]}")
50
  raise ValueError("Generated SVG is invalid")
51
 
52
+ app.logger.debug(f"SVG content length: {len(svg_content)}, first 100 chars: {svg_content[:100]}")
53
  return svg_content
54
 
55
  finally:
56
+ # Clean up
57
  app.logger.debug("Cleaning up temporary files")
58
+ for path in [input_path, output_svg_path]:
59
+ if os.path.exists(path):
60
+ os.remove(path)
 
61
 
62
  @app.route('/convert-to-vector', methods=['POST'])
63
  def convert_to_vector_endpoint():
64
  try:
65
+ # Handle image data
66
  if request.data:
67
  app.logger.debug("Received raw image data")
68
  image = Image.open(io.BytesIO(request.data)).convert('RGB')
 
69
  elif 'image' in request.files:
70
  app.logger.debug("Received multipart image data")
71
  file = request.files['image']
 
74
  app.logger.error("No image data provided")
75
  return jsonify({'error': 'No image data provided'}), 400
76
 
77
+ # Process image
78
  app.logger.debug("Processing image")
79
  svg_content = convert_to_vector(image)
80
 
81
+ # Return SVG
82
  app.logger.debug("Sending SVG response")
83
  return send_file(
84
  io.BytesIO(svg_content.encode('utf-8')),
 
87
  download_name='vector_output.svg'
88
  )
89
  except Exception as e:
90
+ app.logger.error(f"Error processing request: {str(e)}")
91
  return jsonify({'error': str(e)}), 500
92
 
93
  @app.route('/')