|
import io |
|
import gradio as gr |
|
from PIL import Image |
|
import vtracer |
|
import tempfile |
|
|
|
|
|
TRANSLATIONS = { |
|
'en': { |
|
'title': 'Convert Image to SVG Vectors', |
|
'description': 'Upload an image and customize the conversion parameters as needed.' |
|
}, |
|
'de': { |
|
'title': 'Bild in SVG-Vektoren umwandeln', |
|
'description': 'Laden Sie ein Bild hoch und passen Sie die Konvertierungsparameter nach Bedarf an.' |
|
} |
|
} |
|
|
|
def convert_image(image, color_mode, hierarchical, mode, filter_speckle, |
|
color_precision, layer_difference, corner_threshold, |
|
length_threshold, max_iterations, splice_threshold, path_precision, language): |
|
"""Converts an image to SVG using vtracer with customizable parameters.""" |
|
|
|
img_byte_array = io.BytesIO() |
|
image.save(img_byte_array, format='PNG') |
|
img_bytes = img_byte_array.getvalue() |
|
|
|
|
|
svg_str = vtracer.convert_raw_image_to_svg( |
|
img_bytes, |
|
img_format='png', |
|
colormode=color_mode.lower(), |
|
hierarchical=hierarchical.lower(), |
|
mode=mode.lower(), |
|
filter_speckle=int(filter_speckle), |
|
color_precision=int(color_precision), |
|
layer_difference=int(layer_difference), |
|
corner_threshold=int(corner_threshold), |
|
length_threshold=float(length_threshold), |
|
max_iterations=int(max_iterations), |
|
splice_threshold=int(splice_threshold), |
|
path_precision=int(path_precision) |
|
) |
|
|
|
|
|
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg') |
|
temp_file.write(svg_str.encode('utf-8')) |
|
temp_file.close() |
|
|
|
return ( |
|
gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), |
|
temp_file.name |
|
) |
|
|
|
|
|
vector_converter_interface = gr.Blocks() |
|
|
|
with vector_converter_interface: |
|
language_dropdown = gr.Dropdown( |
|
choices=['en', 'de'], |
|
value='en', |
|
label='Language', |
|
type='value' |
|
) |
|
|
|
with gr.Blocks() as interface: |
|
title = gr.Textbox(value=TRANSLATIONS['en']['title'], visible=False) |
|
description = gr.Textbox(value=TRANSLATIONS['en']['description'], visible=False) |
|
|
|
gr.Interface( |
|
fn=convert_image, |
|
inputs=[ |
|
gr.Image(type="pil", label="Upload Image"), |
|
gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"), |
|
gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"), |
|
gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"), |
|
gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"), |
|
gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"), |
|
gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"), |
|
gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"), |
|
gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"), |
|
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"), |
|
gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"), |
|
gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision"), |
|
language_dropdown |
|
], |
|
outputs=[ |
|
gr.HTML(label="SVG Output"), |
|
gr.File(label="Download SVG") |
|
], |
|
title=TRANSLATIONS['en']['title'], |
|
description=TRANSLATIONS['en']['description'] |
|
) |
|
|
|
def update_language(language): |
|
return { |
|
title: TRANSLATIONS[language]['title'], |
|
description: TRANSLATIONS[language]['description'] |
|
} |
|
|
|
language_dropdown.change( |
|
update_language, |
|
inputs=language_dropdown, |
|
outputs=[title, description] |
|
) |
|
|
|
vector_converter_interface.launch() |