Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,30 +1,28 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import vtracer
|
| 3 |
import os
|
| 4 |
-
from PIL import Image
|
| 5 |
|
| 6 |
def convert_to_vector(
|
| 7 |
-
image,
|
| 8 |
-
colormode="color",
|
| 9 |
-
hierarchical="stacked",
|
| 10 |
-
mode="spline",
|
| 11 |
-
filter_speckle=4,
|
| 12 |
-
color_precision=6,
|
| 13 |
-
layer_difference=16,
|
| 14 |
-
corner_threshold=60,
|
| 15 |
-
length_threshold=4.0,
|
| 16 |
-
max_iterations=10,
|
| 17 |
-
splice_threshold=45,
|
| 18 |
path_precision=3
|
| 19 |
):
|
| 20 |
input_path = "temp_input.jpg"
|
| 21 |
output_path = "svg_output.svg"
|
| 22 |
|
| 23 |
-
|
| 24 |
-
return gr.HTML("Please upload an image."), None
|
| 25 |
-
|
| 26 |
image.save(input_path)
|
| 27 |
|
|
|
|
| 28 |
vtracer.convert_image_to_svg_py(
|
| 29 |
input_path,
|
| 30 |
output_path,
|
|
@@ -41,66 +39,48 @@ def convert_to_vector(
|
|
| 41 |
path_precision=int(path_precision)
|
| 42 |
)
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
|
| 51 |
def handle_color_mode(value):
|
| 52 |
return value
|
| 53 |
|
| 54 |
-
|
| 55 |
-
"
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
css = """
|
| 61 |
#col-container {
|
| 62 |
margin: 0 auto;
|
| 63 |
max-width: 960px;
|
| 64 |
-
padding: 20px;
|
| 65 |
}
|
| 66 |
.generate-btn {
|
| 67 |
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
|
| 68 |
border: none !important;
|
| 69 |
color: white !important;
|
| 70 |
-
padding: 10px 20px !important;
|
| 71 |
-
font-size: 16px !important;
|
| 72 |
-
border-radius: 5px !important;
|
| 73 |
-
cursor: pointer !important;
|
| 74 |
-
transition: transform 0.2s, box-shadow 0.2s !important;
|
| 75 |
}
|
| 76 |
.generate-btn:hover {
|
| 77 |
-
transform: translateY(-2px)
|
| 78 |
-
box-shadow: 0 5px 15px rgba(0,0,0,0.2)
|
| 79 |
-
}
|
| 80 |
-
.gr-accordion {
|
| 81 |
-
border: 1px solid #ddd !important;
|
| 82 |
-
border-radius: 5px !important;
|
| 83 |
-
margin-bottom: 10px !important;
|
| 84 |
-
}
|
| 85 |
-
.gr-accordion-title {
|
| 86 |
-
background-color: #f9f9f9 !important;
|
| 87 |
-
padding: 10px !important;
|
| 88 |
-
cursor: pointer !important;
|
| 89 |
-
}
|
| 90 |
-
.gr-accordion-content {
|
| 91 |
-
padding: 15px !important;
|
| 92 |
-
}
|
| 93 |
-
.gr-file {
|
| 94 |
-
border: 1px dashed #aaa !important;
|
| 95 |
-
padding: 10px !important;
|
| 96 |
-
border-radius: 5px !important;
|
| 97 |
}
|
| 98 |
"""
|
| 99 |
|
|
|
|
| 100 |
with gr.Blocks(css=css) as app:
|
| 101 |
with gr.Column(elem_id="col-container"):
|
| 102 |
gr.HTML("""
|
| 103 |
-
<div style="text-align: center;
|
| 104 |
<h2>Image to Vector Converter ⚡</h2>
|
| 105 |
<p>Converts raster images (JPG, PNG, WEBP) to vector graphics (SVG).</p>
|
| 106 |
</div>
|
|
@@ -108,61 +88,75 @@ with gr.Blocks(css=css) as app:
|
|
| 108 |
with gr.Row():
|
| 109 |
with gr.Column():
|
| 110 |
image_input = gr.Image(type="pil", label="Upload Image")
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
colormode = gr.Radio([("COLOR","color"),("B/W", "binary")], value="color", label="Color Mode", show_label=False)
|
| 114 |
-
filter_speckle = gr.Slider(0, 128, value=4, step=1, label="Filter Speckle", info="Cleaner")
|
| 115 |
-
color_precision = gr.Slider(1, 8, value=6, step=1, label="Color Precision", info="More accurate")
|
| 116 |
-
layer_difference = gr.Slider(0, 128, value=16, step=1, label="Gradient Step", info="Less layers")
|
| 117 |
-
hierarchical = gr.Radio([("STACKED","stacked"), ("CUTOUT","cutout")], value="stacked", label="Hierarchical Mode",show_label=False)
|
| 118 |
-
with gr.Accordion("Curve Fitting", open=False):
|
| 119 |
-
mode = gr.Radio([("SPLINE","spline"),("POLYGON", "polygon"), ("PIXEL","none")], value="spline", label="Mode", show_label=False)
|
| 120 |
-
corner_threshold = gr.Slider(0, 180, value=60, step=1, label="Corner Threshold", info="Smoother")
|
| 121 |
-
length_threshold = gr.Slider(3.5, 10, value=4.0, step=0.1, label="Segment Length", info ="More coarse")
|
| 122 |
-
splice_threshold = gr.Slider(0, 180, value=45, step=1, label="Splice Threshold", info="Less accurate")
|
| 123 |
-
max_iterations = gr.Slider(1, 20, value=10, step=1, label="Max Iterations", visible=False)
|
| 124 |
-
path_precision = gr.Slider(1, 10, value=3, step=1, label="Path Precision", visible=False)
|
| 125 |
-
output_text = gr.Textbox(label="Selected Mode", visible=False)
|
| 126 |
-
with gr.Row():
|
| 127 |
-
clear_button = gr.Button("Clear")
|
| 128 |
-
convert_button = gr.Button("✨ Convert to SVG", variant='primary', elem_classes=["generate-btn"])
|
| 129 |
|
| 130 |
with gr.Column():
|
| 131 |
html = gr.HTML(label="SVG Output")
|
| 132 |
svg_output = gr.File(label="Download SVG")
|
| 133 |
-
|
| 134 |
-
gr.
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
inputs=
|
| 157 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
)
|
| 159 |
|
| 160 |
-
|
| 161 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 162 |
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
return (
|
| 166 |
-
gr.update(interactive=is_color_mode),
|
| 167 |
-
gr.update(interactive=is_color_mode),
|
| 168 |
-
gr.update(visible=is_color_mode)
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import vtracer
|
| 3 |
import os
|
|
|
|
| 4 |
|
| 5 |
def convert_to_vector(
|
| 6 |
+
image,
|
| 7 |
+
colormode="color",
|
| 8 |
+
hierarchical="stacked",
|
| 9 |
+
mode="spline",
|
| 10 |
+
filter_speckle=4,
|
| 11 |
+
color_precision=6,
|
| 12 |
+
layer_difference=16,
|
| 13 |
+
corner_threshold=60,
|
| 14 |
+
length_threshold=4.0,
|
| 15 |
+
max_iterations=10,
|
| 16 |
+
splice_threshold=45,
|
| 17 |
path_precision=3
|
| 18 |
):
|
| 19 |
input_path = "temp_input.jpg"
|
| 20 |
output_path = "svg_output.svg"
|
| 21 |
|
| 22 |
+
# Save the input image to a temporary file
|
|
|
|
|
|
|
| 23 |
image.save(input_path)
|
| 24 |
|
| 25 |
+
# Convert the image to SVG using VTracer
|
| 26 |
vtracer.convert_image_to_svg_py(
|
| 27 |
input_path,
|
| 28 |
output_path,
|
|
|
|
| 39 |
path_precision=int(path_precision)
|
| 40 |
)
|
| 41 |
|
| 42 |
+
# Read the SVG output
|
| 43 |
+
with open(output_path, "r") as f:
|
| 44 |
+
svg_content = f.read()
|
| 45 |
+
|
| 46 |
+
# Return the SVG file path and content
|
| 47 |
+
return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>'), output_path
|
| 48 |
|
| 49 |
def handle_color_mode(value):
|
| 50 |
return value
|
| 51 |
|
| 52 |
+
def clear_inputs():
|
| 53 |
+
return None, "color", "stacked", "spline", 4, 6, 16, 60, 4.0, 10, 45, 3
|
| 54 |
+
|
| 55 |
+
def update_interactivity_and_visibility(colormode):
|
| 56 |
+
is_color_mode = colormode == "color"
|
| 57 |
+
return (
|
| 58 |
+
gr.update(interactive=is_color_mode),
|
| 59 |
+
gr.update(interactive=is_color_mode),
|
| 60 |
+
gr.update(visible=is_color_mode)
|
| 61 |
+
)
|
| 62 |
|
| 63 |
css = """
|
| 64 |
#col-container {
|
| 65 |
margin: 0 auto;
|
| 66 |
max-width: 960px;
|
|
|
|
| 67 |
}
|
| 68 |
.generate-btn {
|
| 69 |
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
|
| 70 |
border: none !important;
|
| 71 |
color: white !important;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
}
|
| 73 |
.generate-btn:hover {
|
| 74 |
+
transform: translateY(-2px);
|
| 75 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}
|
| 77 |
"""
|
| 78 |
|
| 79 |
+
# Define the Gradio interface
|
| 80 |
with gr.Blocks(css=css) as app:
|
| 81 |
with gr.Column(elem_id="col-container"):
|
| 82 |
gr.HTML("""
|
| 83 |
+
<div style="text-align: center;">
|
| 84 |
<h2>Image to Vector Converter ⚡</h2>
|
| 85 |
<p>Converts raster images (JPG, PNG, WEBP) to vector graphics (SVG).</p>
|
| 86 |
</div>
|
|
|
|
| 88 |
with gr.Row():
|
| 89 |
with gr.Column():
|
| 90 |
image_input = gr.Image(type="pil", label="Upload Image")
|
| 91 |
+
convert_button = gr.Button("✨ Convert to SVG", variant='primary', elem_classes=["generate-btn"])
|
| 92 |
+
clear_button = gr.Button("Clear Inputs", variant='secondary')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
|
| 94 |
with gr.Column():
|
| 95 |
html = gr.HTML(label="SVG Output")
|
| 96 |
svg_output = gr.File(label="Download SVG")
|
| 97 |
+
|
| 98 |
+
with gr.Row():
|
| 99 |
+
colormode = gr.Radio(choices=["color", "binary"], value="color", label="Color Mode")
|
| 100 |
+
hierarchical = gr.Radio(choices=["stacked", "cutout"], value="stacked", label="Hierarchical Mode")
|
| 101 |
+
mode = gr.Radio(choices=["spline", "polygon"], value="spline", label="Mode")
|
| 102 |
+
|
| 103 |
+
with gr.Row():
|
| 104 |
+
filter_speckle = gr.Slider(minimum=0, maximum=20, value=4, label="Filter Speckle")
|
| 105 |
+
color_precision = gr.Slider(minimum=1, maximum=10, value=6, label="Color Precision")
|
| 106 |
+
layer_difference = gr.Slider(minimum=1, maximum=32, value=16, label="Layer Difference")
|
| 107 |
+
corner_threshold = gr.Slider(minimum=0, maximum=180, value=60, label="Corner Threshold")
|
| 108 |
+
length_threshold = gr.Slider(minimum=0, maximum=10, value=4.0, label="Length Threshold")
|
| 109 |
+
max_iterations = gr.Slider(minimum=1, maximum=20, value=10, label="Max Iterations")
|
| 110 |
+
splice_threshold = gr.Slider(minimum=0, maximum=90, value=45, label="Splice Threshold")
|
| 111 |
+
path_precision = gr.Slider(minimum=1, maximum=10, value=3, label="Path Precision")
|
| 112 |
+
|
| 113 |
+
# Event handlers
|
| 114 |
+
colormode.change(handle_color_mode, inputs=colormode, outputs=colormode)
|
| 115 |
+
hierarchical.change(handle_color_mode, inputs=hierarchical, outputs=hierarchical)
|
| 116 |
+
mode.change(handle_color_mode, inputs=mode, outputs=mode)
|
| 117 |
+
|
| 118 |
+
colormode.change(
|
| 119 |
+
update_interactivity_and_visibility,
|
| 120 |
+
inputs=colormode,
|
| 121 |
+
outputs=[color_precision, layer_difference, hierarchical]
|
| 122 |
+
)
|
| 123 |
+
|
| 124 |
+
clear_button.click(
|
| 125 |
+
clear_inputs,
|
| 126 |
+
outputs=[
|
| 127 |
+
image_input,
|
| 128 |
+
colormode,
|
| 129 |
+
hierarchical,
|
| 130 |
+
mode,
|
| 131 |
+
filter_speckle,
|
| 132 |
+
color_precision,
|
| 133 |
+
layer_difference,
|
| 134 |
+
corner_threshold,
|
| 135 |
+
length_threshold,
|
| 136 |
+
max_iterations,
|
| 137 |
+
splice_threshold,
|
| 138 |
+
path_precision
|
| 139 |
+
]
|
| 140 |
)
|
| 141 |
|
| 142 |
+
convert_button.click(
|
| 143 |
+
convert_to_vector,
|
| 144 |
+
inputs=[
|
| 145 |
+
image_input,
|
| 146 |
+
colormode,
|
| 147 |
+
hierarchical,
|
| 148 |
+
mode,
|
| 149 |
+
filter_speckle,
|
| 150 |
+
color_precision,
|
| 151 |
+
layer_difference,
|
| 152 |
+
corner_threshold,
|
| 153 |
+
length_threshold,
|
| 154 |
+
max_iterations,
|
| 155 |
+
splice_threshold,
|
| 156 |
+
path_precision
|
| 157 |
+
],
|
| 158 |
+
outputs=[html, svg_output]
|
| 159 |
+
)
|
| 160 |
|
| 161 |
+
# Launch the app
|
| 162 |
+
app.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|