Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,38 +12,35 @@ def convert_to_vector(image):
|
|
| 12 |
input_path = "temp_input.jpg"
|
| 13 |
output_path = "temp_output.svg"
|
| 14 |
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
image.save(input_path)
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
|
| 40 |
-
|
| 41 |
-
finally:
|
| 42 |
-
# Clean up temporary files
|
| 43 |
-
if os.path.exists(input_path):
|
| 44 |
-
os.remove(input_path)
|
| 45 |
-
if os.path.exists(output_path):
|
| 46 |
-
os.remove(output_path)
|
| 47 |
|
| 48 |
@app.route('/convert-to-vector', methods=['POST'])
|
| 49 |
def convert_to_vector_endpoint():
|
|
@@ -51,18 +48,23 @@ def convert_to_vector_endpoint():
|
|
| 51 |
# Handle raw image data in the request body
|
| 52 |
if request.data:
|
| 53 |
image = Image.open(io.BytesIO(request.data)).convert('RGB')
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
else:
|
| 55 |
return jsonify({'error': 'No image data provided'}), 400
|
| 56 |
|
| 57 |
# Process the image
|
| 58 |
svg_content = convert_to_vector(image)
|
| 59 |
|
| 60 |
-
# Save the result to a bytes buffer
|
| 61 |
-
svg_io = io.BytesIO(svg_content.encode('utf-8'))
|
| 62 |
-
svg_io.seek(0)
|
| 63 |
-
|
| 64 |
# Return the SVG as a response
|
| 65 |
-
return send_file(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
except Exception as e:
|
| 67 |
return jsonify({'error': str(e)}), 500
|
| 68 |
|
|
|
|
| 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)
|
|
|
|
| 17 |
|
| 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",
|
| 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_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 |
# 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')
|
| 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),
|
| 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
|
| 70 |
|