Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,21 +1,21 @@
|
|
| 1 |
-
from flask import Flask, request, jsonify
|
| 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"/
|
| 10 |
|
| 11 |
def convert_to_vector(image):
|
| 12 |
-
input_path = "temp_input.
|
| 13 |
-
output_path = "
|
| 14 |
|
| 15 |
-
# Save input image
|
| 16 |
image.save(input_path)
|
| 17 |
|
| 18 |
-
# Convert to SVG using
|
| 19 |
vtracer.convert_image_to_svg_py(
|
| 20 |
input_path,
|
| 21 |
output_path,
|
|
@@ -32,36 +32,45 @@ def convert_to_vector(image):
|
|
| 32 |
path_precision=3
|
| 33 |
)
|
| 34 |
|
| 35 |
-
# Read SVG content
|
| 36 |
-
with open(output_path, "
|
| 37 |
svg_content = f.read()
|
| 38 |
|
| 39 |
-
# Clean up
|
| 40 |
os.remove(input_path)
|
| 41 |
os.remove(output_path)
|
| 42 |
|
| 43 |
return svg_content
|
| 44 |
|
| 45 |
-
@app.route('/
|
| 46 |
-
def
|
| 47 |
try:
|
| 48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
return jsonify({'error': 'No image data provided'}), 400
|
| 50 |
|
| 51 |
-
#
|
| 52 |
-
image = Image.open(io.BytesIO(request.data)).convert('RGB')
|
| 53 |
-
|
| 54 |
-
# Convert to SVG
|
| 55 |
svg_content = convert_to_vector(image)
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
except Exception as e:
|
| 60 |
return jsonify({'error': str(e)}), 500
|
| 61 |
|
| 62 |
@app.route('/')
|
| 63 |
def index():
|
| 64 |
-
return jsonify({'status': 'Image
|
| 65 |
|
| 66 |
if __name__ == '__main__':
|
| 67 |
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 io
|
| 5 |
+
import vtracer
|
| 6 |
import os
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
+
CORS(app, resources={r"/vectorize": {"origins": "*"}})
|
| 10 |
|
| 11 |
def convert_to_vector(image):
|
| 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 settings
|
| 19 |
vtracer.convert_image_to_svg_py(
|
| 20 |
input_path,
|
| 21 |
output_path,
|
|
|
|
| 32 |
path_precision=3
|
| 33 |
)
|
| 34 |
|
| 35 |
+
# Read the SVG content
|
| 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('/vectorize', methods=['POST'])
|
| 46 |
+
def vectorize_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 to SVG
|
|
|
|
|
|
|
|
|
|
| 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='vectorized.svg'
|
| 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 Vectorization API is running'})
|
| 74 |
|
| 75 |
if __name__ == '__main__':
|
| 76 |
app.run(host='0.0.0.0', port=7860)
|