Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from svgtrace import trace_image
|
3 |
+
import io
|
4 |
+
|
5 |
+
def convert_image_to_svg(image, threshold=128, line_thickness=1.0):
|
6 |
+
# Convert the uploaded image to bytes
|
7 |
+
image_bytes = io.BytesIO()
|
8 |
+
image.save(image_bytes, format='PNG')
|
9 |
+
image_bytes = image_bytes.getvalue()
|
10 |
+
|
11 |
+
# Convert the image to SVG using custom tracing options
|
12 |
+
svg = trace_image(image_bytes, threshold=threshold, line_thickness=line_thickness)
|
13 |
+
|
14 |
+
# Save the SVG output to a file
|
15 |
+
output_svg = 'output_image.svg'
|
16 |
+
with open(output_svg, 'w') as f:
|
17 |
+
f.write(svg)
|
18 |
+
|
19 |
+
return output_svg
|
20 |
+
|
21 |
+
# Set up Gradio interface with customizable options
|
22 |
+
def create_gradio_interface():
|
23 |
+
inputs = [
|
24 |
+
gr.inputs.Image(type="pil", label="Upload Image"),
|
25 |
+
gr.inputs.Slider(0, 255, default=128, label="Threshold"),
|
26 |
+
gr.inputs.Slider(0.1, 5.0, default=1.0, label="Line Thickness")
|
27 |
+
]
|
28 |
+
|
29 |
+
outputs = gr.outputs.File(label="Download SVG")
|
30 |
+
|
31 |
+
interface = gr.Interface(
|
32 |
+
fn=convert_image_to_svg,
|
33 |
+
inputs=inputs,
|
34 |
+
outputs=outputs,
|
35 |
+
title="Image to SVG Converter",
|
36 |
+
description="Upload an image and customize the options to convert it to a high-quality SVG file."
|
37 |
+
)
|
38 |
+
|
39 |
+
return interface
|
40 |
+
|
41 |
+
# Launch the Gradio interface
|
42 |
+
if __name__ == "__main__":
|
43 |
+
interface = create_gradio_interface()
|
44 |
+
interface.launch()
|