Update app.py
Browse files
app.py
CHANGED
@@ -4,39 +4,24 @@ 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 |
-
'upload_label': 'Upload Image',
|
13 |
-
'convert_btn': 'Convert to SVG',
|
14 |
-
'svg_output': 'SVG Output',
|
15 |
-
'download_svg': 'Download SVG'
|
16 |
},
|
17 |
'de': {
|
18 |
'title': 'Bild in SVG-Vektoren umwandeln',
|
19 |
-
'description': 'Laden Sie ein Bild hoch und passen Sie die Konvertierungsparameter nach Bedarf an.'
|
20 |
-
'upload_label': 'Bild hochladen',
|
21 |
-
'convert_btn': 'In SVG umwandeln',
|
22 |
-
'svg_output': 'SVG-Ausgabe',
|
23 |
-
'download_svg': 'SVG herunterladen'
|
24 |
}
|
25 |
}
|
26 |
|
27 |
def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
28 |
-
|
29 |
-
|
30 |
-
"""Converts an image to SVG using vtracer with customizable parameters."""
|
31 |
-
if image is None:
|
32 |
-
return None, None
|
33 |
-
|
34 |
-
# Convert Gradio image to bytes for vtracer compatibility
|
35 |
img_byte_array = io.BytesIO()
|
36 |
image.save(img_byte_array, format='PNG')
|
37 |
img_bytes = img_byte_array.getvalue()
|
38 |
|
39 |
-
# Perform the conversion
|
40 |
svg_str = vtracer.convert_raw_image_to_svg(
|
41 |
img_bytes,
|
42 |
img_format='png',
|
@@ -53,30 +38,26 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
|
53 |
path_precision=int(path_precision)
|
54 |
)
|
55 |
|
56 |
-
# Save the SVG string to a temporary file
|
57 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
|
58 |
temp_file.write(svg_str.encode('utf-8'))
|
59 |
temp_file.close()
|
60 |
|
61 |
return (
|
62 |
-
f'<svg viewBox="0 0 {image.width} {image.height}"
|
63 |
temp_file.name
|
64 |
)
|
65 |
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
TRANSLATIONS[language]['upload_label'],
|
72 |
-
TRANSLATIONS[language]['convert_btn'],
|
73 |
-
TRANSLATIONS[language]['svg_output'],
|
74 |
-
TRANSLATIONS[language]['download_svg']
|
75 |
)
|
76 |
|
77 |
-
|
78 |
-
|
79 |
-
|
|
|
80 |
fn=convert_image,
|
81 |
inputs=[
|
82 |
gr.Image(type="pil", label="Upload Image"),
|
@@ -90,18 +71,24 @@ def create_interface():
|
|
90 |
gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
|
91 |
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
|
92 |
gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
|
93 |
-
gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
|
|
|
94 |
],
|
95 |
outputs=[
|
96 |
gr.HTML(label="SVG Output"),
|
97 |
gr.File(label="Download SVG")
|
98 |
],
|
99 |
-
title=
|
100 |
-
description=
|
101 |
)
|
102 |
|
103 |
-
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
107 |
-
vector_converter_interface.launch()
|
|
|
4 |
import vtracer
|
5 |
import tempfile
|
6 |
|
|
|
7 |
TRANSLATIONS = {
|
8 |
'en': {
|
9 |
'title': 'Convert Image to SVG Vectors',
|
10 |
+
'description': 'Upload an image and customize the conversion parameters as needed.'
|
|
|
|
|
|
|
|
|
11 |
},
|
12 |
'de': {
|
13 |
'title': 'Bild in SVG-Vektoren umwandeln',
|
14 |
+
'description': 'Laden Sie ein Bild hoch und passen Sie die Konvertierungsparameter nach Bedarf an.'
|
|
|
|
|
|
|
|
|
15 |
}
|
16 |
}
|
17 |
|
18 |
def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
19 |
+
color_precision, layer_difference, corner_threshold,
|
20 |
+
length_threshold, max_iterations, splice_threshold, path_precision, language):
|
|
|
|
|
|
|
|
|
|
|
21 |
img_byte_array = io.BytesIO()
|
22 |
image.save(img_byte_array, format='PNG')
|
23 |
img_bytes = img_byte_array.getvalue()
|
24 |
|
|
|
25 |
svg_str = vtracer.convert_raw_image_to_svg(
|
26 |
img_bytes,
|
27 |
img_format='png',
|
|
|
38 |
path_precision=int(path_precision)
|
39 |
)
|
40 |
|
|
|
41 |
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
|
42 |
temp_file.write(svg_str.encode('utf-8'))
|
43 |
temp_file.close()
|
44 |
|
45 |
return (
|
46 |
+
gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'),
|
47 |
temp_file.name
|
48 |
)
|
49 |
|
50 |
+
with gr.Blocks() as vector_converter_interface:
|
51 |
+
language_dropdown = gr.Dropdown(
|
52 |
+
choices=['en', 'de'],
|
53 |
+
value='en',
|
54 |
+
label='Language'
|
|
|
|
|
|
|
|
|
55 |
)
|
56 |
|
57 |
+
title = gr.Textbox(value=TRANSLATIONS['en']['title'], visible=False)
|
58 |
+
description = gr.Textbox(value=TRANSLATIONS['en']['description'], visible=False)
|
59 |
+
|
60 |
+
gr.Interface(
|
61 |
fn=convert_image,
|
62 |
inputs=[
|
63 |
gr.Image(type="pil", label="Upload Image"),
|
|
|
71 |
gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
|
72 |
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
|
73 |
gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
|
74 |
+
gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision"),
|
75 |
+
language_dropdown
|
76 |
],
|
77 |
outputs=[
|
78 |
gr.HTML(label="SVG Output"),
|
79 |
gr.File(label="Download SVG")
|
80 |
],
|
81 |
+
title=TRANSLATIONS['en']['title'],
|
82 |
+
description=TRANSLATIONS['en']['description']
|
83 |
)
|
84 |
|
85 |
+
def update_language(language):
|
86 |
+
return TRANSLATIONS[language]['title'], TRANSLATIONS[language]['description']
|
87 |
+
|
88 |
+
language_dropdown.change(
|
89 |
+
update_language,
|
90 |
+
inputs=language_dropdown,
|
91 |
+
outputs=[title, description]
|
92 |
+
)
|
93 |
|
94 |
+
vector_converter_interface.launch()
|
|