Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,68 +1,81 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
from PIL import Image
|
| 4 |
import vtracer
|
| 5 |
-
import io
|
| 6 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = Flask(__name__)
|
| 9 |
-
CORS(app
|
| 10 |
-
|
| 11 |
-
def convert_to_vector(image, colormode="color", hierarchical="stacked", mode="spline", filter_speckle=4, color_precision=6, layer_difference=16, corner_threshold=60, length_threshold=4.0, max_iterations=10, splice_threshold=45, path_precision=3):
|
| 12 |
-
input_path = "temp_input.jpg"
|
| 13 |
-
output_path = "svg_output.svg"
|
| 14 |
-
|
| 15 |
-
# Save the input image to a temporary file
|
| 16 |
-
image.save(input_path)
|
| 17 |
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
input_path,
|
| 21 |
-
output_path,
|
| 22 |
-
colormode=colormode,
|
| 23 |
-
hierarchical=hierarchical,
|
| 24 |
-
mode=mode,
|
| 25 |
-
filter_speckle=int(filter_speckle),
|
| 26 |
-
color_precision=int(color_precision),
|
| 27 |
-
layer_difference=int(layer_difference),
|
| 28 |
-
corner_threshold=int(corner_threshold),
|
| 29 |
-
length_threshold=float(length_threshold),
|
| 30 |
-
max_iterations=int(max_iterations),
|
| 31 |
-
splice_threshold=int(splice_threshold),
|
| 32 |
-
path_precision=int(path_precision)
|
| 33 |
-
)
|
| 34 |
-
|
| 35 |
-
# Read the SVG output
|
| 36 |
-
with open(output_path, "r") 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():
|
| 47 |
try:
|
| 48 |
-
#
|
| 49 |
-
if request.
|
| 50 |
-
image
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
-
return jsonify({
|
| 62 |
-
|
| 63 |
-
@app.route('/')
|
| 64 |
-
def index():
|
| 65 |
-
return jsonify({'status': 'Image to Vector API is running'})
|
| 66 |
|
| 67 |
-
if __name__ ==
|
| 68 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
|
| 1 |
+
|
| 2 |
+
import gradio as gr
|
|
|
|
| 3 |
import vtracer
|
|
|
|
| 4 |
import os
|
| 5 |
+
import base64
|
| 6 |
+
from io import BytesIO
|
| 7 |
+
from PIL import Image
|
| 8 |
+
import json
|
| 9 |
+
from flask import Flask, request, jsonify
|
| 10 |
+
from flask_cors import CORS
|
| 11 |
|
| 12 |
app = Flask(__name__)
|
| 13 |
+
CORS(app)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
|
| 15 |
+
@app.route('/api/predict', methods=['POST'])
|
| 16 |
+
def predict():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
try:
|
| 18 |
+
# Get image file from request
|
| 19 |
+
if 'image' not in request.files:
|
| 20 |
+
return jsonify({"error": "No image file provided"}), 400
|
| 21 |
+
|
| 22 |
+
image_file = request.files['image']
|
| 23 |
+
image = Image.open(image_file)
|
| 24 |
+
|
| 25 |
+
# Get parameters from request
|
| 26 |
+
colormode = request.form.get('colormode', 'color')
|
| 27 |
+
hierarchical = request.form.get('hierarchical', 'stacked')
|
| 28 |
+
mode = request.form.get('mode', 'spline')
|
| 29 |
+
filter_speckle = int(request.form.get('filter_speckle', 4))
|
| 30 |
+
color_precision = int(request.form.get('color_precision', 6))
|
| 31 |
+
layer_difference = int(request.form.get('layer_difference', 16))
|
| 32 |
+
corner_threshold = int(request.form.get('corner_threshold', 60))
|
| 33 |
+
length_threshold = float(request.form.get('length_threshold', 4.0))
|
| 34 |
+
max_iterations = int(request.form.get('max_iterations', 10))
|
| 35 |
+
splice_threshold = int(request.form.get('splice_threshold', 45))
|
| 36 |
+
path_precision = int(request.form.get('path_precision', 3))
|
| 37 |
+
|
| 38 |
+
# Create temp directory if it doesn't exist
|
| 39 |
+
os.makedirs('temp', exist_ok=True)
|
| 40 |
+
|
| 41 |
+
# Save the input image to a temporary file
|
| 42 |
+
input_path = "temp/temp_input.jpg"
|
| 43 |
+
output_path = "temp/svg_output.svg"
|
| 44 |
+
|
| 45 |
+
image.save(input_path)
|
| 46 |
+
|
| 47 |
+
# Convert the image to SVG using VTracer
|
| 48 |
+
vtracer.convert_image_to_svg_py(
|
| 49 |
+
input_path,
|
| 50 |
+
output_path,
|
| 51 |
+
colormode=colormode,
|
| 52 |
+
hierarchical=hierarchical,
|
| 53 |
+
mode=mode,
|
| 54 |
+
filter_speckle=filter_speckle,
|
| 55 |
+
color_precision=color_precision,
|
| 56 |
+
layer_difference=layer_difference,
|
| 57 |
+
corner_threshold=corner_threshold,
|
| 58 |
+
length_threshold=length_threshold,
|
| 59 |
+
max_iterations=max_iterations,
|
| 60 |
+
splice_threshold=splice_threshold,
|
| 61 |
+
path_precision=path_precision
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
# Read the SVG output
|
| 65 |
+
with open(output_path, "r") as f:
|
| 66 |
+
svg_content = f.read()
|
| 67 |
+
|
| 68 |
+
# Create SVG with viewBox
|
| 69 |
+
full_svg = f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>'
|
| 70 |
+
|
| 71 |
+
# Return SVG content and path
|
| 72 |
+
return jsonify({
|
| 73 |
+
"data": [full_svg, output_path],
|
| 74 |
+
"success": True
|
| 75 |
+
})
|
| 76 |
+
|
| 77 |
except Exception as e:
|
| 78 |
+
return jsonify({"error": str(e), "success": False}), 500
|
|
|
|
|
|
|
|
|
|
|
|
|
| 79 |
|
| 80 |
+
if __name__ == "__main__":
|
| 81 |
+
app.run(host='0.0.0.0', port=7860)
|