Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,24 @@
|
|
| 1 |
from flask import Flask, request, jsonify, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
from PIL import Image
|
| 4 |
-
import io
|
| 5 |
import vtracer
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
-
CORS(app, resources={r"/
|
| 10 |
|
| 11 |
-
def convert_to_vector(
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
mode="spline",
|
| 16 |
-
filter_speckle=4,
|
| 17 |
-
color_precision=6,
|
| 18 |
-
layer_difference=16,
|
| 19 |
-
corner_threshold=60,
|
| 20 |
-
length_threshold=4.0,
|
| 21 |
-
max_iterations=10,
|
| 22 |
-
splice_threshold=45,
|
| 23 |
-
path_precision=3
|
| 24 |
-
):
|
| 25 |
input_path = "temp_input.jpg"
|
| 26 |
output_path = "temp_output.svg"
|
| 27 |
|
|
|
|
| 28 |
image.save(input_path)
|
| 29 |
|
|
|
|
| 30 |
vtracer.convert_image_to_svg_py(
|
| 31 |
input_path,
|
| 32 |
output_path,
|
|
@@ -43,39 +35,41 @@ def convert_to_vector(
|
|
| 43 |
path_precision=int(path_precision)
|
| 44 |
)
|
| 45 |
|
| 46 |
-
|
|
|
|
| 47 |
svg_content = f.read()
|
| 48 |
|
|
|
|
| 49 |
os.remove(input_path)
|
| 50 |
os.remove(output_path)
|
| 51 |
|
| 52 |
return svg_content
|
| 53 |
|
| 54 |
-
@app.route('/
|
| 55 |
-
def
|
| 56 |
try:
|
|
|
|
| 57 |
if request.data:
|
| 58 |
image = Image.open(io.BytesIO(request.data)).convert('RGB')
|
| 59 |
-
elif 'image' in request.files:
|
| 60 |
-
file = request.files['image']
|
| 61 |
-
image = Image.open(file).convert('RGB')
|
| 62 |
else:
|
| 63 |
return jsonify({'error': 'No image data provided'}), 400
|
| 64 |
|
|
|
|
| 65 |
svg_content = convert_to_vector(image)
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
| 73 |
except Exception as e:
|
| 74 |
return jsonify({'error': str(e)}), 500
|
| 75 |
|
| 76 |
@app.route('/')
|
| 77 |
def index():
|
| 78 |
-
return jsonify({'status': 'Image
|
| 79 |
|
| 80 |
if __name__ == '__main__':
|
| 81 |
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
from flask import Flask, request, jsonify, send_file
|
| 2 |
from flask_cors import CORS
|
| 3 |
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, colormode="color", hierarchical="stacked", mode="spline",
|
| 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 |
|
| 18 |
+
# Save the input image to a temporary file
|
| 19 |
image.save(input_path)
|
| 20 |
|
| 21 |
+
# Convert the image to SVG using VTracer
|
| 22 |
vtracer.convert_image_to_svg_py(
|
| 23 |
input_path,
|
| 24 |
output_path,
|
|
|
|
| 35 |
path_precision=int(path_precision)
|
| 36 |
)
|
| 37 |
|
| 38 |
+
# Read the SVG output
|
| 39 |
+
with open(output_path, "r") as f:
|
| 40 |
svg_content = f.read()
|
| 41 |
|
| 42 |
+
# Clean up temporary files
|
| 43 |
os.remove(input_path)
|
| 44 |
os.remove(output_path)
|
| 45 |
|
| 46 |
return svg_content
|
| 47 |
|
| 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 |
+
# Convert image to SVG with default parameters (can be extended with query params if needed)
|
| 58 |
svg_content = convert_to_vector(image)
|
| 59 |
|
| 60 |
+
# Save SVG content to a bytes buffer
|
| 61 |
+
svg_io = io.StringIO(svg_content)
|
| 62 |
+
svg_bytes = io.BytesIO(svg_content.encode('utf-8'))
|
| 63 |
+
svg_bytes.seek(0)
|
| 64 |
+
|
| 65 |
+
# Return the SVG as a file response
|
| 66 |
+
return send_file(svg_bytes, mimetype='image/svg+xml', as_attachment=True, download_name='vector_output.svg')
|
| 67 |
except Exception as e:
|
| 68 |
return jsonify({'error': str(e)}), 500
|
| 69 |
|
| 70 |
@app.route('/')
|
| 71 |
def index():
|
| 72 |
+
return jsonify({'status': 'Image to Vector Converter API is running'})
|
| 73 |
|
| 74 |
if __name__ == '__main__':
|
| 75 |
app.run(host='0.0.0.0', port=7860)
|