Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,124 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import io
|
2 |
+
import gradio as gr
|
3 |
+
from PIL import Image
|
4 |
+
import vtracer
|
5 |
+
import tempfile
|
6 |
+
|
7 |
+
# Localization dictionary
|
8 |
+
TRANSLATIONS = {
|
9 |
+
'en': {
|
10 |
+
'title': 'Convert Image to SVG Vectors',
|
11 |
+
'description': 'Upload an image and customize the conversion parameters as needed.'
|
12 |
+
},
|
13 |
+
'de': {
|
14 |
+
'title': 'Bild in SVG-Vektoren umwandeln',
|
15 |
+
'description': 'Laden Sie ein Bild hoch und passen Sie die Konvertierungsparameter nach Bedarf an.'
|
16 |
+
}
|
17 |
+
}
|
18 |
+
|
19 |
+
def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
20 |
+
color_precision, layer_difference, corner_threshold,
|
21 |
+
length_threshold, max_iterations, splice_threshold, path_precision):
|
22 |
+
"""Converts an image to SVG using vtracer with customizable parameters."""
|
23 |
+
# Convert Gradio image to bytes for vtracer compatibility
|
24 |
+
img_byte_array = io.BytesIO()
|
25 |
+
image.save(img_byte_array, format='PNG')
|
26 |
+
img_bytes = img_byte_array.getvalue()
|
27 |
+
|
28 |
+
# Perform the conversion
|
29 |
+
svg_str = vtracer.convert_raw_image_to_svg(
|
30 |
+
img_bytes,
|
31 |
+
img_format='png',
|
32 |
+
colormode=color_mode.lower(),
|
33 |
+
hierarchical=hierarchical.lower(),
|
34 |
+
mode=mode.lower(),
|
35 |
+
filter_speckle=int(filter_speckle),
|
36 |
+
color_precision=int(color_precision),
|
37 |
+
layer_difference=int(layer_difference),
|
38 |
+
corner_threshold=int(corner_threshold),
|
39 |
+
length_threshold=float(length_threshold),
|
40 |
+
max_iterations=int(max_iterations),
|
41 |
+
splice_threshold=int(splice_threshold),
|
42 |
+
path_precision=int(path_precision)
|
43 |
+
)
|
44 |
+
|
45 |
+
# Save the SVG string to a temporary file
|
46 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
|
47 |
+
temp_file.write(svg_str.encode('utf-8'))
|
48 |
+
temp_file.close()
|
49 |
+
|
50 |
+
return (
|
51 |
+
gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'),
|
52 |
+
temp_file.name
|
53 |
+
)
|
54 |
+
|
55 |
+
# Create Gradio interface
|
56 |
+
with gr.Blocks() as vector_converter_interface:
|
57 |
+
# Language selector at the top
|
58 |
+
language_dropdown = gr.Dropdown(
|
59 |
+
choices=['en', 'de'],
|
60 |
+
value='en',
|
61 |
+
label='Language / Sprache'
|
62 |
+
)
|
63 |
+
|
64 |
+
# Title and description that will be updated
|
65 |
+
title_display = gr.Markdown(f"# {TRANSLATIONS['en']['title']}")
|
66 |
+
description_display = gr.Markdown(TRANSLATIONS['en']['description'])
|
67 |
+
|
68 |
+
# Main interface components
|
69 |
+
with gr.Row():
|
70 |
+
with gr.Column():
|
71 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
72 |
+
|
73 |
+
with gr.Column():
|
74 |
+
color_mode = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
|
75 |
+
hierarchical = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical")
|
76 |
+
mode = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode")
|
77 |
+
|
78 |
+
with gr.Row():
|
79 |
+
with gr.Column():
|
80 |
+
filter_speckle = gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle")
|
81 |
+
color_precision = gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision")
|
82 |
+
layer_difference = gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference")
|
83 |
+
corner_threshold = gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold")
|
84 |
+
|
85 |
+
with gr.Column():
|
86 |
+
length_threshold = gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold")
|
87 |
+
max_iterations = gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations")
|
88 |
+
splice_threshold = gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold")
|
89 |
+
path_precision = gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
|
90 |
+
|
91 |
+
# Convert button
|
92 |
+
convert_btn = gr.Button("Convert to SVG", variant="primary")
|
93 |
+
|
94 |
+
# Output components
|
95 |
+
with gr.Row():
|
96 |
+
svg_output = gr.HTML(label="SVG Output")
|
97 |
+
file_output = gr.File(label="Download SVG")
|
98 |
+
|
99 |
+
# Function to update language
|
100 |
+
def update_language(language):
|
101 |
+
new_title = f"# {TRANSLATIONS[language]['title']}"
|
102 |
+
new_description = TRANSLATIONS[language]['description']
|
103 |
+
return new_title, new_description
|
104 |
+
|
105 |
+
# Connect the language dropdown to update title and description
|
106 |
+
language_dropdown.change(
|
107 |
+
fn=update_language,
|
108 |
+
inputs=[language_dropdown],
|
109 |
+
outputs=[title_display, description_display]
|
110 |
+
)
|
111 |
+
|
112 |
+
# Connect the convert button to the conversion function
|
113 |
+
convert_btn.click(
|
114 |
+
fn=convert_image,
|
115 |
+
inputs=[
|
116 |
+
image_input, color_mode, hierarchical, mode, filter_speckle,
|
117 |
+
color_precision, layer_difference, corner_threshold,
|
118 |
+
length_threshold, max_iterations, splice_threshold, path_precision
|
119 |
+
],
|
120 |
+
outputs=[svg_output, file_output]
|
121 |
+
)
|
122 |
+
|
123 |
+
if __name__ == "__main__":
|
124 |
+
vector_converter_interface.launch()
|