Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -8,10 +8,7 @@ import os
|
|
| 8 |
app = Flask(__name__)
|
| 9 |
CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
|
| 10 |
|
| 11 |
-
def convert_to_vector(image
|
| 12 |
-
filter_speckle=4, color_precision=6, layer_difference=16,
|
| 13 |
-
corner_threshold=60, length_threshold=4.0, max_iterations=10,
|
| 14 |
-
splice_threshold=45, path_precision=3):
|
| 15 |
input_path = "temp_input.jpg"
|
| 16 |
output_path = "temp_output.svg"
|
| 17 |
|
|
@@ -19,21 +16,21 @@ def convert_to_vector(image, colormode="color", hierarchical="stacked", mode="sp
|
|
| 19 |
# Save the input image to a temporary file
|
| 20 |
image.save(input_path)
|
| 21 |
|
| 22 |
-
# Convert the image to SVG using VTracer
|
| 23 |
vtracer.convert_image_to_svg_py(
|
| 24 |
input_path,
|
| 25 |
output_path,
|
| 26 |
-
colormode=
|
| 27 |
-
hierarchical=
|
| 28 |
-
mode=
|
| 29 |
-
filter_speckle=
|
| 30 |
-
color_precision=
|
| 31 |
-
layer_difference=
|
| 32 |
-
corner_threshold=
|
| 33 |
-
length_threshold=
|
| 34 |
-
max_iterations=
|
| 35 |
-
splice_threshold=
|
| 36 |
-
path_precision=
|
| 37 |
)
|
| 38 |
|
| 39 |
# Read the SVG output
|
|
@@ -51,24 +48,25 @@ def convert_to_vector(image, colormode="color", hierarchical="stacked", mode="sp
|
|
| 51 |
@app.route('/convert-to-vector', methods=['POST'])
|
| 52 |
def convert_to_vector_endpoint():
|
| 53 |
try:
|
| 54 |
-
#
|
| 55 |
-
if
|
|
|
|
|
|
|
| 56 |
return jsonify({'error': 'No image data provided'}), 400
|
| 57 |
|
| 58 |
-
#
|
| 59 |
-
image = Image.open(io.BytesIO(request.data)).convert('RGB')
|
| 60 |
-
|
| 61 |
-
# Convert to SVG
|
| 62 |
svg_content = convert_to_vector(image)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
|
|
|
|
|
|
| 68 |
except Exception as e:
|
| 69 |
return jsonify({'error': str(e)}), 500
|
| 70 |
|
| 71 |
-
@app.route('/'
|
| 72 |
def index():
|
| 73 |
return jsonify({'status': 'Image to Vector Converter API is running'})
|
| 74 |
|
|
|
|
| 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_path = "temp_output.svg"
|
| 14 |
|
|
|
|
| 16 |
# Save the input image to a temporary file
|
| 17 |
image.save(input_path)
|
| 18 |
|
| 19 |
+
# Convert the image to SVG using VTracer with default parameters from Gradio app
|
| 20 |
vtracer.convert_image_to_svg_py(
|
| 21 |
input_path,
|
| 22 |
output_path,
|
| 23 |
+
colormode="color",
|
| 24 |
+
hierarchical="stacked",
|
| 25 |
+
mode="spline",
|
| 26 |
+
filter_speckle=4,
|
| 27 |
+
color_precision=6,
|
| 28 |
+
layer_difference=16,
|
| 29 |
+
corner_threshold=60,
|
| 30 |
+
length_threshold=4.0,
|
| 31 |
+
max_iterations=10,
|
| 32 |
+
splice_threshold=45,
|
| 33 |
+
path_precision=3
|
| 34 |
)
|
| 35 |
|
| 36 |
# Read the SVG output
|
|
|
|
| 48 |
@app.route('/convert-to-vector', methods=['POST'])
|
| 49 |
def convert_to_vector_endpoint():
|
| 50 |
try:
|
| 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(svg_io, mimetype='image/svg+xml', as_attachment=True, download_name='vector_output.svg')
|
| 66 |
except Exception as e:
|
| 67 |
return jsonify({'error': str(e)}), 500
|
| 68 |
|
| 69 |
+
@app.route('/')
|
| 70 |
def index():
|
| 71 |
return jsonify({'status': 'Image to Vector Converter API is running'})
|
| 72 |
|