Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -4,75 +4,72 @@ 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
|
| 12 |
input_path = "temp_input.jpg"
|
| 13 |
output_svg_path = "temp_output.svg"
|
|
|
|
| 14 |
|
|
|
|
| 15 |
image.save(input_path)
|
| 16 |
|
|
|
|
| 17 |
vtracer.convert_image_to_svg_py(
|
| 18 |
input_path,
|
| 19 |
output_svg_path,
|
| 20 |
-
colormode=
|
| 21 |
-
hierarchical=
|
| 22 |
-
mode=
|
| 23 |
-
filter_speckle=
|
| 24 |
-
color_precision=
|
| 25 |
-
layer_difference=
|
| 26 |
-
corner_threshold=
|
| 27 |
-
length_threshold=
|
| 28 |
-
max_iterations=
|
| 29 |
-
splice_threshold=
|
| 30 |
-
path_precision=
|
| 31 |
)
|
| 32 |
|
| 33 |
-
|
| 34 |
-
|
| 35 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 36 |
os.remove(input_path)
|
| 37 |
os.remove(output_svg_path)
|
|
|
|
| 38 |
|
| 39 |
-
return
|
| 40 |
|
| 41 |
@app.route('/convert-to-vector', methods=['POST'])
|
| 42 |
def convert_to_vector_endpoint():
|
| 43 |
try:
|
| 44 |
-
|
|
|
|
| 45 |
image = Image.open(io.BytesIO(request.data)).convert('RGB')
|
|
|
|
| 46 |
elif 'image' in request.files:
|
| 47 |
file = request.files['image']
|
| 48 |
image = Image.open(file).convert('RGB')
|
| 49 |
else:
|
| 50 |
return jsonify({'error': 'No image data provided'}), 400
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
else:
|
| 55 |
-
params = {
|
| 56 |
-
"colormode": request.args.get("colormode", "color"),
|
| 57 |
-
"hierarchical": request.args.get("hierarchical", "stacked"),
|
| 58 |
-
"mode": request.args.get("mode", "spline"),
|
| 59 |
-
"filter_speckle": request.args.get("filter_speckle", 4),
|
| 60 |
-
"color_precision": request.args.get("color_precision", 6),
|
| 61 |
-
"layer_difference": request.args.get("layer_difference", 16),
|
| 62 |
-
"corner_threshold": request.args.get("corner_threshold", 60),
|
| 63 |
-
"length_threshold": request.args.get("length_threshold", 4.0),
|
| 64 |
-
"max_iterations": request.args.get("max_iterations", 10),
|
| 65 |
-
"splice_threshold": request.args.get("splice_threshold", 45),
|
| 66 |
-
"path_precision": request.args.get("path_precision", 3)
|
| 67 |
-
}
|
| 68 |
-
|
| 69 |
-
svg_content = convert_to_vector(image, params)
|
| 70 |
|
|
|
|
| 71 |
return send_file(
|
| 72 |
-
io.BytesIO(
|
| 73 |
-
mimetype='image/
|
| 74 |
as_attachment=True,
|
| 75 |
-
download_name='vector_output.
|
| 76 |
)
|
| 77 |
except Exception as e:
|
| 78 |
return jsonify({'error': str(e)}), 500
|
|
|
|
| 4 |
import vtracer
|
| 5 |
import io
|
| 6 |
import os
|
| 7 |
+
import cairosvg
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
CORS(app, resources={r"/convert-to-vector": {"origins": "*"}})
|
| 11 |
|
| 12 |
+
def convert_to_vector(image):
|
| 13 |
input_path = "temp_input.jpg"
|
| 14 |
output_svg_path = "temp_output.svg"
|
| 15 |
+
output_png_path = "temp_output.png"
|
| 16 |
|
| 17 |
+
# Save the input image to a temporary file
|
| 18 |
image.save(input_path)
|
| 19 |
|
| 20 |
+
# Convert the image to SVG using VTracer with default parameters
|
| 21 |
vtracer.convert_image_to_svg_py(
|
| 22 |
input_path,
|
| 23 |
output_svg_path,
|
| 24 |
+
colormode="color",
|
| 25 |
+
hierarchical="stacked",
|
| 26 |
+
mode="spline",
|
| 27 |
+
filter_speckle=4,
|
| 28 |
+
color_precision=6,
|
| 29 |
+
layer_difference=16,
|
| 30 |
+
corner_threshold=60,
|
| 31 |
+
length_threshold=4.0,
|
| 32 |
+
max_iterations=10,
|
| 33 |
+
splice_threshold=45,
|
| 34 |
+
path_precision=3
|
| 35 |
)
|
| 36 |
|
| 37 |
+
# Convert SVG to PNG
|
| 38 |
+
cairosvg.svg2png(url=output_svg_path, write_to=output_png_path)
|
| 39 |
|
| 40 |
+
# Read the PNG output
|
| 41 |
+
with open(output_png_path, "rb") as f:
|
| 42 |
+
png_content = f.read()
|
| 43 |
+
|
| 44 |
+
# Clean up temporary files
|
| 45 |
os.remove(input_path)
|
| 46 |
os.remove(output_svg_path)
|
| 47 |
+
os.remove(output_png_path)
|
| 48 |
|
| 49 |
+
return png_content
|
| 50 |
|
| 51 |
@app.route('/convert-to-vector', methods=['POST'])
|
| 52 |
def convert_to_vector_endpoint():
|
| 53 |
try:
|
| 54 |
+
# Handle raw image data in the request body
|
| 55 |
+
if request.data:
|
| 56 |
image = Image.open(io.BytesIO(request.data)).convert('RGB')
|
| 57 |
+
# Fallback for multipart/form-data
|
| 58 |
elif 'image' in request.files:
|
| 59 |
file = request.files['image']
|
| 60 |
image = Image.open(file).convert('RGB')
|
| 61 |
else:
|
| 62 |
return jsonify({'error': 'No image data provided'}), 400
|
| 63 |
|
| 64 |
+
# Process the image
|
| 65 |
+
png_content = convert_to_vector(image)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
+
# Return the PNG as a response
|
| 68 |
return send_file(
|
| 69 |
+
io.BytesIO(png_content),
|
| 70 |
+
mimetype='image/png',
|
| 71 |
as_attachment=True,
|
| 72 |
+
download_name='vector_output.png'
|
| 73 |
)
|
| 74 |
except Exception as e:
|
| 75 |
return jsonify({'error': str(e)}), 500
|