Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,28 +1,30 @@
|
|
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 |
-
|
|
|
|
|
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,
|
@@ -38,19 +40,17 @@ def convert_to_vector(
|
|
38 |
splice_threshold=int(splice_threshold),
|
39 |
path_precision=int(path_precision)
|
40 |
)
|
41 |
-
# Read the SVG output
|
42 |
-
with open(output_path, "r") as f:
|
43 |
-
svg_content = f.read()
|
44 |
|
45 |
-
|
46 |
-
|
47 |
-
|
|
|
|
|
|
|
48 |
|
49 |
def handle_color_mode(value):
|
50 |
-
# You can change this to display the selected value without any prefix
|
51 |
return value
|
52 |
|
53 |
-
|
54 |
examples = [
|
55 |
"examples/11.jpg",
|
56 |
"examples/02.jpg",
|
@@ -61,27 +61,50 @@ css = """
|
|
61 |
#col-container {
|
62 |
margin: 0 auto;
|
63 |
max-width: 960px;
|
|
|
64 |
}
|
65 |
.generate-btn {
|
66 |
background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
|
67 |
border: none !important;
|
68 |
color: white !important;
|
|
|
|
|
|
|
|
|
|
|
69 |
}
|
70 |
.generate-btn:hover {
|
71 |
-
transform: translateY(-2px);
|
72 |
-
box-shadow: 0 5px 15px rgba(0,0,0,0.2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
}
|
74 |
"""
|
75 |
|
76 |
-
# Define the Gradio interface
|
77 |
with gr.Blocks(css=css) as app:
|
78 |
with gr.Column(elem_id="col-container"):
|
79 |
gr.HTML("""
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
with gr.Row():
|
86 |
with gr.Column():
|
87 |
image_input = gr.Image(type="pil", label="Upload Image")
|
@@ -105,7 +128,7 @@ with gr.Blocks(css=css) as app:
|
|
105 |
convert_button = gr.Button("✨ Convert to SVG", variant='primary', elem_classes=["generate-btn"])
|
106 |
|
107 |
with gr.Column():
|
108 |
-
html = gr.HTML(label="SVG Output")
|
109 |
svg_output = gr.File(label="Download SVG")
|
110 |
|
111 |
gr.Examples(
|
@@ -116,25 +139,20 @@ with gr.Blocks(css=css) as app:
|
|
116 |
cache_examples=False,
|
117 |
run_on_click = True
|
118 |
)
|
119 |
-
|
120 |
colormode.change(handle_color_mode, inputs=colormode,outputs=output_text)
|
121 |
hierarchical.change(handle_color_mode, inputs=hierarchical,outputs=output_text)
|
122 |
mode.change(handle_color_mode, inputs=mode,outputs=output_text)
|
123 |
-
default_values = {
|
124 |
-
"color_precision": 6,
|
125 |
-
"layer_difference": 16
|
126 |
-
}
|
127 |
|
128 |
def clear_inputs():
|
129 |
return gr.Image(value=None), gr.Radio(value="color"), gr.Radio(value="stacked"), gr.Radio(value="spline"), gr.Slider(value=4), gr.Slider(value=6), gr.Slider(value=16), gr.Slider(value=60), gr.Slider(value=4.0), gr.Slider(value=10), gr.Slider(value=45), gr.Slider(value=3)
|
130 |
|
131 |
-
|
132 |
def update_interactivity_and_visibility(colormode, color_precision_value, layer_difference_value):
|
133 |
is_color_mode = colormode == "color"
|
134 |
return (
|
135 |
gr.update(interactive=is_color_mode),
|
136 |
gr.update(interactive=is_color_mode),
|
137 |
-
gr.update(visible=is_color_mode)
|
138 |
)
|
139 |
|
140 |
colormode.change(
|
@@ -154,45 +172,4 @@ with gr.Blocks(css=css) as app:
|
|
154 |
mode.change(
|
155 |
update_interactivity_and_visibility_for_mode,
|
156 |
inputs=[mode],
|
157 |
-
outputs=[corner_threshold,length_threshold,
|
158 |
-
)
|
159 |
-
|
160 |
-
clear_button.click(
|
161 |
-
clear_inputs,
|
162 |
-
outputs=[
|
163 |
-
image_input,
|
164 |
-
colormode,
|
165 |
-
hierarchical,
|
166 |
-
mode,
|
167 |
-
filter_speckle,
|
168 |
-
color_precision,
|
169 |
-
layer_difference,
|
170 |
-
corner_threshold,
|
171 |
-
length_threshold,
|
172 |
-
max_iterations,
|
173 |
-
splice_threshold,
|
174 |
-
path_precision
|
175 |
-
]
|
176 |
-
)
|
177 |
-
|
178 |
-
convert_button.click(
|
179 |
-
convert_to_vector,
|
180 |
-
inputs=[
|
181 |
-
image_input,
|
182 |
-
colormode,
|
183 |
-
hierarchical,
|
184 |
-
mode,
|
185 |
-
filter_speckle,
|
186 |
-
color_precision,
|
187 |
-
layer_difference,
|
188 |
-
corner_threshold,
|
189 |
-
length_threshold,
|
190 |
-
max_iterations,
|
191 |
-
splice_threshold,
|
192 |
-
path_precision
|
193 |
-
],
|
194 |
-
outputs=[html,svg_output]
|
195 |
-
)
|
196 |
-
|
197 |
-
# Launch the app
|
198 |
-
app.launch(debug=True)
|
|
|
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 |
+
if image is None:
|
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,
|
|
|
40 |
splice_threshold=int(splice_threshold),
|
41 |
path_precision=int(path_precision)
|
42 |
)
|
|
|
|
|
|
|
43 |
|
44 |
+
try:
|
45 |
+
with open(output_path, "r") as f:
|
46 |
+
svg_content = f.read()
|
47 |
+
return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>'), output_path
|
48 |
+
except FileNotFoundError:
|
49 |
+
return gr.HTML("SVG conversion failed."), None
|
50 |
|
51 |
def handle_color_mode(value):
|
|
|
52 |
return value
|
53 |
|
|
|
54 |
examples = [
|
55 |
"examples/11.jpg",
|
56 |
"examples/02.jpg",
|
|
|
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) !important;
|
78 |
+
box-shadow: 0 5px 15px rgba(0,0,0,0.2) !important;
|
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; margin-bottom: 20px;">
|
104 |
+
<h2>Image to Vector Converter ⚡</h2>
|
105 |
+
<p>Converts raster images (JPG, PNG, WEBP) to vector graphics (SVG).</p>
|
106 |
+
</div>
|
107 |
+
""")
|
108 |
with gr.Row():
|
109 |
with gr.Column():
|
110 |
image_input = gr.Image(type="pil", label="Upload Image")
|
|
|
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.Examples(
|
|
|
139 |
cache_examples=False,
|
140 |
run_on_click = True
|
141 |
)
|
142 |
+
|
143 |
colormode.change(handle_color_mode, inputs=colormode,outputs=output_text)
|
144 |
hierarchical.change(handle_color_mode, inputs=hierarchical,outputs=output_text)
|
145 |
mode.change(handle_color_mode, inputs=mode,outputs=output_text)
|
|
|
|
|
|
|
|
|
146 |
|
147 |
def clear_inputs():
|
148 |
return gr.Image(value=None), gr.Radio(value="color"), gr.Radio(value="stacked"), gr.Radio(value="spline"), gr.Slider(value=4), gr.Slider(value=6), gr.Slider(value=16), gr.Slider(value=60), gr.Slider(value=4.0), gr.Slider(value=10), gr.Slider(value=45), gr.Slider(value=3)
|
149 |
|
|
|
150 |
def update_interactivity_and_visibility(colormode, color_precision_value, layer_difference_value):
|
151 |
is_color_mode = colormode == "color"
|
152 |
return (
|
153 |
gr.update(interactive=is_color_mode),
|
154 |
gr.update(interactive=is_color_mode),
|
155 |
+
gr.update(visible=is_color_mode)
|
156 |
)
|
157 |
|
158 |
colormode.change(
|
|
|
172 |
mode.change(
|
173 |
update_interactivity_and_visibility_for_mode,
|
174 |
inputs=[mode],
|
175 |
+
outputs=[corner_threshold,length_threshold,splice
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|