Spaces:
Sleeping
Sleeping
Upload 15 files
Browse files- .gitattributes +10 -0
- README.md +9 -9
- app.py +198 -27
- examples/01.jpg +3 -0
- examples/02.jpg +3 -0
- examples/03.jpg +3 -0
- examples/04.jpg +3 -0
- examples/05.jpg +3 -0
- examples/06.jpg +3 -0
- examples/07.png +3 -0
- examples/08.jpg +3 -0
- examples/09.jpg +3 -0
- examples/10.jpg +3 -0
- examples/11.jpg +0 -0
- requirements.txt +1 -5
.gitattributes
CHANGED
|
@@ -33,3 +33,13 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
examples/01.jpg filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
examples/02.jpg filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
examples/03.jpg filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
examples/04.jpg filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
examples/05.jpg filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
examples/06.jpg filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
examples/07.png filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
examples/08.jpg filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
examples/09.jpg filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
examples/10.jpg filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
|
@@ -1,13 +1,13 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
-
sdk:
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
short_description:
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Image To Vector
|
| 3 |
+
emoji: 🚀
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 5.25.2
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
+
short_description: Vectorizer AI | Convert Image to SVG
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
app.py
CHANGED
|
@@ -1,27 +1,198 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,
|
| 29 |
+
colormode=colormode,
|
| 30 |
+
hierarchical=hierarchical,
|
| 31 |
+
mode=mode,
|
| 32 |
+
filter_speckle=int(filter_speckle),
|
| 33 |
+
color_precision=int(color_precision),
|
| 34 |
+
layer_difference=int(layer_difference),
|
| 35 |
+
corner_threshold=int(corner_threshold),
|
| 36 |
+
length_threshold=float(length_threshold),
|
| 37 |
+
max_iterations=int(max_iterations),
|
| 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 |
+
# Return the SVG file path instead of content
|
| 46 |
+
return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>'), output_path
|
| 47 |
+
# return output_path,output_path
|
| 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",
|
| 57 |
+
"examples/03.jpg",
|
| 58 |
+
]
|
| 59 |
+
|
| 60 |
+
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 |
+
<div style="text-align: center;">
|
| 81 |
+
<h2>Image to Vector Converter ⚡</h2>
|
| 82 |
+
<p>Converts raster images (JPG, PNG, WEBP) to vector graphics (SVG).</p>
|
| 83 |
+
</div>
|
| 84 |
+
""")
|
| 85 |
+
with gr.Row():
|
| 86 |
+
with gr.Column():
|
| 87 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 88 |
+
with gr.Accordion("Advanced Settings", open=False):
|
| 89 |
+
with gr.Accordion("Clustering", open=False):
|
| 90 |
+
colormode = gr.Radio([("COLOR","color"),("B/W", "binary")], value="color", label="Color Mode", show_label=False)
|
| 91 |
+
filter_speckle = gr.Slider(0, 128, value=4, step=1, label="Filter Speckle", info="Cleaner")
|
| 92 |
+
color_precision = gr.Slider(1, 8, value=6, step=1, label="Color Precision", info="More accurate")
|
| 93 |
+
layer_difference = gr.Slider(0, 128, value=16, step=1, label="Gradient Step", info="Less layers")
|
| 94 |
+
hierarchical = gr.Radio([("STACKED","stacked"), ("CUTOUT","cutout")], value="stacked", label="Hierarchical Mode",show_label=False)
|
| 95 |
+
with gr.Accordion("Curve Fitting", open=False):
|
| 96 |
+
mode = gr.Radio([("SPLINE","spline"),("POLYGON", "polygon"), ("PIXEL","none")], value="spline", label="Mode", show_label=False)
|
| 97 |
+
corner_threshold = gr.Slider(0, 180, value=60, step=1, label="Corner Threshold", info="Smoother")
|
| 98 |
+
length_threshold = gr.Slider(3.5, 10, value=4.0, step=0.1, label="Segment Length", info ="More coarse")
|
| 99 |
+
splice_threshold = gr.Slider(0, 180, value=45, step=1, label="Splice Threshold", info="Less accurate")
|
| 100 |
+
max_iterations = gr.Slider(1, 20, value=10, step=1, label="Max Iterations", visible=False)
|
| 101 |
+
path_precision = gr.Slider(1, 10, value=3, step=1, label="Path Precision", visible=False)
|
| 102 |
+
output_text = gr.Textbox(label="Selected Mode", visible=False)
|
| 103 |
+
with gr.Row():
|
| 104 |
+
clear_button = gr.Button("Clear")
|
| 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") # container=True, show_label=True
|
| 109 |
+
svg_output = gr.File(label="Download SVG")
|
| 110 |
+
|
| 111 |
+
gr.Examples(
|
| 112 |
+
examples = examples,
|
| 113 |
+
fn = convert_to_vector,
|
| 114 |
+
inputs = [image_input],
|
| 115 |
+
outputs = [html,svg_output],
|
| 116 |
+
cache_examples=False,
|
| 117 |
+
run_on_click = True
|
| 118 |
+
)
|
| 119 |
+
# Store default values for restoration
|
| 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) # Show/Hide Hierarchical Mode
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
colormode.change(
|
| 141 |
+
update_interactivity_and_visibility,
|
| 142 |
+
inputs=[colormode, color_precision, layer_difference],
|
| 143 |
+
outputs=[color_precision, layer_difference, hierarchical]
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
def update_interactivity_and_visibility_for_mode(mode):
|
| 147 |
+
is_spline_mode = mode == "spline"
|
| 148 |
+
return (
|
| 149 |
+
gr.update(interactive=is_spline_mode),
|
| 150 |
+
gr.update(interactive=is_spline_mode),
|
| 151 |
+
gr.update(interactive=is_spline_mode)
|
| 152 |
+
)
|
| 153 |
+
|
| 154 |
+
mode.change(
|
| 155 |
+
update_interactivity_and_visibility_for_mode,
|
| 156 |
+
inputs=[mode],
|
| 157 |
+
outputs=[corner_threshold,length_threshold,splice_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)
|
examples/01.jpg
ADDED
|
Git LFS Details
|
examples/02.jpg
ADDED
|
Git LFS Details
|
examples/03.jpg
ADDED
|
Git LFS Details
|
examples/04.jpg
ADDED
|
Git LFS Details
|
examples/05.jpg
ADDED
|
Git LFS Details
|
examples/06.jpg
ADDED
|
Git LFS Details
|
examples/07.png
ADDED
|
Git LFS Details
|
examples/08.jpg
ADDED
|
Git LFS Details
|
examples/09.jpg
ADDED
|
Git LFS Details
|
examples/10.jpg
ADDED
|
Git LFS Details
|
examples/11.jpg
ADDED
|
requirements.txt
CHANGED
|
@@ -1,5 +1 @@
|
|
| 1 |
-
|
| 2 |
-
transformers
|
| 3 |
-
Pillow
|
| 4 |
-
torchvision
|
| 5 |
-
scikit-image
|
|
|
|
| 1 |
+
vtracer
|
|
|
|
|
|
|
|
|
|
|
|