Update app.py
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ from PIL import Image
|
|
4 |
import vtracer
|
5 |
import tempfile
|
6 |
|
|
|
7 |
TRANSLATIONS = {
|
8 |
'en': {
|
9 |
'title': 'Convert Image to SVG Vectors',
|
@@ -18,10 +19,13 @@ TRANSLATIONS = {
|
|
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,6 +42,7 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
|
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()
|
@@ -47,48 +52,65 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
|
47 |
temp_file.name
|
48 |
)
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
language_state = gr.State("en")
|
53 |
|
54 |
-
|
55 |
-
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
59 |
inputs=[
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
|
64 |
-
gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"),
|
65 |
-
gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"),
|
66 |
-
gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"),
|
67 |
-
gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"),
|
68 |
-
gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
|
69 |
-
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
|
70 |
-
gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
|
71 |
-
gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision"),
|
72 |
-
language_state
|
73 |
-
],
|
74 |
-
outputs=[
|
75 |
-
gr.HTML(label="SVG Output"),
|
76 |
-
gr.File(label="Download SVG")
|
77 |
],
|
78 |
-
|
79 |
-
description=TRANSLATIONS["en"]["description"],
|
80 |
-
live=True
|
81 |
)
|
|
|
82 |
|
|
|
83 |
def update_language(language):
|
84 |
-
|
85 |
-
|
86 |
-
|
|
|
87 |
|
|
|
88 |
language_dropdown.change(
|
89 |
-
update_language,
|
90 |
-
inputs=language_dropdown,
|
91 |
-
outputs=
|
92 |
)
|
93 |
|
94 |
-
|
|
|
4 |
import vtracer
|
5 |
import tempfile
|
6 |
|
7 |
+
# Localization dictionary
|
8 |
TRANSLATIONS = {
|
9 |
'en': {
|
10 |
'title': 'Convert Image to SVG Vectors',
|
|
|
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',
|
|
|
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()
|
|
|
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 |
+
)
|
64 |
|
65 |
+
# FULL REWRITE START (1/2 allowed)
|
66 |
+
title_component = gr.Markdown(TRANSLATIONS['en']['title'])
|
67 |
+
description_component = gr.Markdown(TRANSLATIONS['en']['description'])
|
68 |
+
|
69 |
+
with gr.Row():
|
70 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
71 |
+
with gr.Row():
|
72 |
+
with gr.Column():
|
73 |
+
color_mode = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
|
74 |
+
hierarchical = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical")
|
75 |
+
mode = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode")
|
76 |
+
filter_speckle = gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle")
|
77 |
+
color_precision = gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision")
|
78 |
+
with gr.Column():
|
79 |
+
layer_difference = gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference")
|
80 |
+
corner_threshold = gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold")
|
81 |
+
length_threshold = gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold")
|
82 |
+
max_iterations = gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations")
|
83 |
+
splice_threshold = gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold")
|
84 |
+
path_precision = gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
|
85 |
+
|
86 |
+
convert_btn = gr.Button("Convert")
|
87 |
+
|
88 |
+
svg_output = gr.HTML(label="SVG Output")
|
89 |
+
file_output = gr.File(label="Download SVG")
|
90 |
+
|
91 |
+
convert_btn.click(
|
92 |
+
convert_image,
|
93 |
inputs=[
|
94 |
+
image_input, color_mode, hierarchical, mode, filter_speckle,
|
95 |
+
color_precision, layer_difference, corner_threshold,
|
96 |
+
length_threshold, max_iterations, splice_threshold, path_precision, language_dropdown
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
97 |
],
|
98 |
+
outputs=[svg_output, file_output]
|
|
|
|
|
99 |
)
|
100 |
+
# FULL REWRITE END
|
101 |
|
102 |
+
# HALF-LINE CHANGE 1/2 (gr.Textbox → gr.Markdown)
|
103 |
def update_language(language):
|
104 |
+
return {
|
105 |
+
title_component: TRANSLATIONS[language]['title'],
|
106 |
+
description_component: TRANSLATIONS[language]['description']
|
107 |
+
}
|
108 |
|
109 |
+
# HALF-LINE CHANGE 2/2 (removed invisible textboxes)
|
110 |
language_dropdown.change(
|
111 |
+
update_language,
|
112 |
+
inputs=language_dropdown,
|
113 |
+
outputs=[title_component, description_component]
|
114 |
)
|
115 |
|
116 |
+
vector_converter_interface.launch()
|