Update app.py
Browse files
app.py
CHANGED
|
@@ -8,18 +8,29 @@ import tempfile
|
|
| 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 |
-
|
| 21 |
-
|
| 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')
|
|
@@ -48,77 +59,49 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
|
| 48 |
temp_file.close()
|
| 49 |
|
| 50 |
return (
|
| 51 |
-
|
| 52 |
temp_file.name
|
| 53 |
)
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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 |
-
|
| 113 |
-
|
|
|
|
| 114 |
fn=convert_image,
|
| 115 |
inputs=[
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
],
|
| 120 |
-
outputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
)
|
| 122 |
|
|
|
|
|
|
|
|
|
|
| 123 |
if __name__ == "__main__":
|
| 124 |
-
vector_converter_interface.launch(
|
|
|
|
| 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 |
+
color_precision, layer_difference, corner_threshold,
|
| 29 |
+
length_threshold, max_iterations, splice_threshold, path_precision):
|
| 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')
|
|
|
|
| 59 |
temp_file.close()
|
| 60 |
|
| 61 |
return (
|
| 62 |
+
f'<svg viewBox="0 0 {image.width} {image.height}" style="max-width: 100%; height: auto;">{svg_str}</svg>',
|
| 63 |
temp_file.name
|
| 64 |
)
|
| 65 |
|
| 66 |
+
def update_interface(language):
|
| 67 |
+
"""Update interface language"""
|
| 68 |
+
return (
|
| 69 |
+
f"# {TRANSLATIONS[language]['title']}",
|
| 70 |
+
TRANSLATIONS[language]['description'],
|
| 71 |
+
TRANSLATIONS[language]['upload_label'],
|
| 72 |
+
TRANSLATIONS[language]['convert_btn'],
|
| 73 |
+
TRANSLATIONS[language]['svg_output'],
|
| 74 |
+
TRANSLATIONS[language]['download_svg']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
)
|
| 76 |
+
|
| 77 |
+
# Create the interface using gr.Interface instead of gr.Blocks
|
| 78 |
+
def create_interface():
|
| 79 |
+
return gr.Interface(
|
| 80 |
fn=convert_image,
|
| 81 |
inputs=[
|
| 82 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 83 |
+
gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
|
| 84 |
+
gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
|
| 85 |
+
gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
|
| 86 |
+
gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"),
|
| 87 |
+
gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"),
|
| 88 |
+
gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"),
|
| 89 |
+
gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"),
|
| 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="Convert Image to SVG Vectors / Bild in SVG-Vektoren umwandeln",
|
| 100 |
+
description="Upload an image and customize the conversion parameters as needed. / Laden Sie ein Bild hoch und passen Sie die Konvertierungsparameter nach Bedarf an."
|
| 101 |
)
|
| 102 |
|
| 103 |
+
# Create and launch the interface
|
| 104 |
+
vector_converter_interface = create_interface()
|
| 105 |
+
|
| 106 |
if __name__ == "__main__":
|
| 107 |
+
vector_converter_interface.launch()
|