Update app.py
Browse files
app.py
CHANGED
|
@@ -4,7 +4,7 @@ from PIL import Image
|
|
| 4 |
import vtracer
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
-
#
|
| 8 |
TRANSLATIONS = {
|
| 9 |
'en': {
|
| 10 |
'title': '# Convert Image to SVG Vectors',
|
|
@@ -50,6 +50,7 @@ TRANSLATIONS = {
|
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
|
|
|
| 53 |
def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
| 54 |
color_precision, layer_difference, corner_threshold,
|
| 55 |
length_threshold, max_iterations, splice_threshold, path_precision):
|
|
@@ -80,23 +81,15 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
|
| 80 |
temp_file.write(svg_str)
|
| 81 |
temp_file.close()
|
| 82 |
|
| 83 |
-
# Simple preview, as full SVG rendering can be complex in gr.HTML
|
| 84 |
preview_html = f"<div style='background-color:#f0f0f0; padding:10px; border-radius:5px;'>SVG successfully generated. Preview:<br><img src='/file={temp_file.name}' width='300'></div>"
|
| 85 |
|
| 86 |
return preview_html, temp_file.name
|
| 87 |
|
| 88 |
-
|
| 89 |
with gr.Blocks() as vector_converter_interface:
|
| 90 |
-
|
| 91 |
-
lang = 'de'
|
| 92 |
|
| 93 |
-
language_dropdown = gr.Dropdown(
|
| 94 |
-
choices=['en', 'de'],
|
| 95 |
-
value=lang,
|
| 96 |
-
label=TRANSLATIONS[lang]['lang_label'],
|
| 97 |
-
interactive=True
|
| 98 |
-
)
|
| 99 |
-
|
| 100 |
title = gr.Markdown(value=TRANSLATIONS[lang]['title'])
|
| 101 |
description = gr.Markdown(value=TRANSLATIONS[lang]['description'])
|
| 102 |
|
|
@@ -104,9 +97,12 @@ with gr.Blocks() as vector_converter_interface:
|
|
| 104 |
with gr.Column(scale=1):
|
| 105 |
image_input = gr.Image(type="pil", label=TRANSLATIONS[lang]['image_label'])
|
| 106 |
|
| 107 |
-
|
|
|
|
|
|
|
| 108 |
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label=TRANSLATIONS[lang]['color_mode_label'])
|
| 109 |
hierarchical_input = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label=TRANSLATIONS[lang]['hierarchical_label'])
|
|
|
|
| 110 |
mode_input = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label=TRANSLATIONS[lang]['mode_label'])
|
| 111 |
filter_speckle_input = gr.Slider(minimum=1, maximum=10, value=4, step=1, label=TRANSLATIONS[lang]['filter_speckle_label'])
|
| 112 |
color_precision_input = gr.Slider(minimum=1, maximum=8, value=6, step=1, label=TRANSLATIONS[lang]['color_precision_label'])
|
|
@@ -123,7 +119,6 @@ with gr.Blocks() as vector_converter_interface:
|
|
| 123 |
svg_output = gr.HTML(label=TRANSLATIONS[lang]['output_svg_label'])
|
| 124 |
file_output = gr.File(label=TRANSLATIONS[lang]['download_svg_label'])
|
| 125 |
|
| 126 |
-
# --- 3. Die Update-Funktion, die alle UI-Elemente aktualisiert ---
|
| 127 |
def update_language(language):
|
| 128 |
t = TRANSLATIONS[language]
|
| 129 |
return {
|
|
@@ -131,8 +126,8 @@ with gr.Blocks() as vector_converter_interface:
|
|
| 131 |
title: gr.update(value=t['title']),
|
| 132 |
description: gr.update(value=t['description']),
|
| 133 |
image_input: gr.update(label=t['image_label']),
|
| 134 |
-
#
|
| 135 |
-
gr.
|
| 136 |
color_mode_input: gr.update(label=t['color_mode_label']),
|
| 137 |
hierarchical_input: gr.update(label=t['hierarchical_label']),
|
| 138 |
mode_input: gr.update(label=t['mode_label']),
|
|
@@ -149,7 +144,6 @@ with gr.Blocks() as vector_converter_interface:
|
|
| 149 |
file_output: gr.update(label=t['download_svg_label']),
|
| 150 |
}
|
| 151 |
|
| 152 |
-
# --- 4. Event-Handler verbinden ---
|
| 153 |
all_inputs = [
|
| 154 |
image_input, color_mode_input, hierarchical_input, mode_input,
|
| 155 |
filter_speckle_input, color_precision_input, layer_difference_input,
|
|
@@ -158,20 +152,15 @@ with gr.Blocks() as vector_converter_interface:
|
|
| 158 |
]
|
| 159 |
all_outputs = [svg_output, file_output]
|
| 160 |
|
| 161 |
-
|
| 162 |
-
convert_button.click(
|
| 163 |
-
fn=convert_image,
|
| 164 |
-
inputs=all_inputs,
|
| 165 |
-
outputs=all_outputs
|
| 166 |
-
)
|
| 167 |
|
| 168 |
-
#
|
| 169 |
ui_components_to_update = [
|
| 170 |
-
language_dropdown, title, description, image_input,
|
| 171 |
-
hierarchical_input, mode_input, filter_speckle_input,
|
| 172 |
-
layer_difference_input, corner_threshold_input,
|
| 173 |
-
max_iterations_input, splice_threshold_input,
|
| 174 |
-
convert_button, svg_output, file_output
|
| 175 |
]
|
| 176 |
language_dropdown.change(
|
| 177 |
fn=update_language,
|
|
|
|
| 4 |
import vtracer
|
| 5 |
import tempfile
|
| 6 |
|
| 7 |
+
# Unverändertes TRANSLATIONS-Wörterbuch...
|
| 8 |
TRANSLATIONS = {
|
| 9 |
'en': {
|
| 10 |
'title': '# Convert Image to SVG Vectors',
|
|
|
|
| 50 |
}
|
| 51 |
}
|
| 52 |
|
| 53 |
+
# Unveränderte convert_image Funktion...
|
| 54 |
def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
| 55 |
color_precision, layer_difference, corner_threshold,
|
| 56 |
length_threshold, max_iterations, splice_threshold, path_precision):
|
|
|
|
| 81 |
temp_file.write(svg_str)
|
| 82 |
temp_file.close()
|
| 83 |
|
|
|
|
| 84 |
preview_html = f"<div style='background-color:#f0f0f0; padding:10px; border-radius:5px;'>SVG successfully generated. Preview:<br><img src='/file={temp_file.name}' width='300'></div>"
|
| 85 |
|
| 86 |
return preview_html, temp_file.name
|
| 87 |
|
| 88 |
+
|
| 89 |
with gr.Blocks() as vector_converter_interface:
|
| 90 |
+
lang = 'en' # Start mit Englisch
|
|
|
|
| 91 |
|
| 92 |
+
language_dropdown = gr.Dropdown(choices=['en', 'de'], value=lang, label=TRANSLATIONS[lang]['lang_label'], interactive=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 93 |
title = gr.Markdown(value=TRANSLATIONS[lang]['title'])
|
| 94 |
description = gr.Markdown(value=TRANSLATIONS[lang]['description'])
|
| 95 |
|
|
|
|
| 97 |
with gr.Column(scale=1):
|
| 98 |
image_input = gr.Image(type="pil", label=TRANSLATIONS[lang]['image_label'])
|
| 99 |
|
| 100 |
+
# --- ÄNDERUNG 1: Variable für Accordion hinzugefügt ---
|
| 101 |
+
advanced_accordion = gr.Accordion(TRANSLATIONS[lang]['advanced_label'], open=False)
|
| 102 |
+
with advanced_accordion:
|
| 103 |
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label=TRANSLATIONS[lang]['color_mode_label'])
|
| 104 |
hierarchical_input = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label=TRANSLATIONS[lang]['hierarchical_label'])
|
| 105 |
+
# ... restliche Inputs unverändert ...
|
| 106 |
mode_input = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label=TRANSLATIONS[lang]['mode_label'])
|
| 107 |
filter_speckle_input = gr.Slider(minimum=1, maximum=10, value=4, step=1, label=TRANSLATIONS[lang]['filter_speckle_label'])
|
| 108 |
color_precision_input = gr.Slider(minimum=1, maximum=8, value=6, step=1, label=TRANSLATIONS[lang]['color_precision_label'])
|
|
|
|
| 119 |
svg_output = gr.HTML(label=TRANSLATIONS[lang]['output_svg_label'])
|
| 120 |
file_output = gr.File(label=TRANSLATIONS[lang]['download_svg_label'])
|
| 121 |
|
|
|
|
| 122 |
def update_language(language):
|
| 123 |
t = TRANSLATIONS[language]
|
| 124 |
return {
|
|
|
|
| 126 |
title: gr.update(value=t['title']),
|
| 127 |
description: gr.update(value=t['description']),
|
| 128 |
image_input: gr.update(label=t['image_label']),
|
| 129 |
+
# --- ÄNDERUNG 2: Korrekte Referenz auf die Accordion-Variable ---
|
| 130 |
+
advanced_accordion: gr.update(label=t['advanced_label']),
|
| 131 |
color_mode_input: gr.update(label=t['color_mode_label']),
|
| 132 |
hierarchical_input: gr.update(label=t['hierarchical_label']),
|
| 133 |
mode_input: gr.update(label=t['mode_label']),
|
|
|
|
| 144 |
file_output: gr.update(label=t['download_svg_label']),
|
| 145 |
}
|
| 146 |
|
|
|
|
| 147 |
all_inputs = [
|
| 148 |
image_input, color_mode_input, hierarchical_input, mode_input,
|
| 149 |
filter_speckle_input, color_precision_input, layer_difference_input,
|
|
|
|
| 152 |
]
|
| 153 |
all_outputs = [svg_output, file_output]
|
| 154 |
|
| 155 |
+
convert_button.click(fn=convert_image, inputs=all_inputs, outputs=all_outputs)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
|
| 157 |
+
# --- ÄNDERUNG 3: Accordion-Variable zur Output-Liste hinzugefügt ---
|
| 158 |
ui_components_to_update = [
|
| 159 |
+
language_dropdown, title, description, image_input, advanced_accordion,
|
| 160 |
+
color_mode_input, hierarchical_input, mode_input, filter_speckle_input,
|
| 161 |
+
color_precision_input, layer_difference_input, corner_threshold_input,
|
| 162 |
+
length_threshold_input, max_iterations_input, splice_threshold_input,
|
| 163 |
+
path_precision_input, convert_button, svg_output, file_output
|
| 164 |
]
|
| 165 |
language_dropdown.change(
|
| 166 |
fn=update_language,
|