Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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, language):
|
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 |
+
# Gradio interface
|
56 |
+
vector_converter_interface = gr.Blocks()
|
57 |
+
|
58 |
+
with vector_converter_interface:
|
59 |
+
language_dropdown = gr.Dropdown(
|
60 |
+
choices=['en', 'de'],
|
61 |
+
value='en',
|
62 |
+
label='Language',
|
63 |
+
type='value'
|
64 |
+
)
|
65 |
+
|
66 |
+
interface = gr.Interface(
|
67 |
+
fn=convert_image,
|
68 |
+
inputs=[
|
69 |
+
gr.Image(type="pil", label="Upload Image"),
|
70 |
+
gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
|
71 |
+
gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
|
72 |
+
gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
|
73 |
+
gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"),
|
74 |
+
gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"),
|
75 |
+
gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"),
|
76 |
+
gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"),
|
77 |
+
gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
|
78 |
+
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
|
79 |
+
gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
|
80 |
+
gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision"),
|
81 |
+
language_dropdown
|
82 |
+
],
|
83 |
+
outputs=[
|
84 |
+
gr.HTML(label="SVG Output"),
|
85 |
+
gr.File(label="Download SVG")
|
86 |
+
],
|
87 |
+
title=gr.State(TRANSLATIONS['en']['title']),
|
88 |
+
description=gr.State(TRANSLATIONS['en']['description'])
|
89 |
+
)
|
90 |
+
|
91 |
+
def update_language(language):
|
92 |
+
return {
|
93 |
+
interface.title: TRANSLATIONS[language]['title'],
|
94 |
+
interface.description: TRANSLATIONS[language]['description']
|
95 |
+
}
|
96 |
+
|
97 |
+
language_dropdown.change(
|
98 |
+
update_language,
|
99 |
+
inputs=language_dropdown,
|
100 |
+
outputs=[interface.title, interface.description]
|
101 |
+
)
|
102 |
+
|
103 |
+
vector_converter_interface.launch()
|