File size: 4,597 Bytes
5e2fbf6
 
 
 
 
 
bb06cc3
5e2fbf6
 
 
57de3e4
5e2fbf6
 
 
57de3e4
5e2fbf6
 
 
 
57de3e4
 
bb06cc3
 
5e2fbf6
 
 
 
bb06cc3
5e2fbf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb06cc3
5e2fbf6
 
 
 
 
57de3e4
5e2fbf6
 
 
bb06cc3
 
53fb129
bb06cc3
 
 
 
 
 
57de3e4
bb06cc3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5e2fbf6
bb06cc3
 
 
53fb129
bb06cc3
5e2fbf6
bb06cc3
5e2fbf6
bb06cc3
57de3e4
bb06cc3
 
 
 
57de3e4
bb06cc3
57de3e4
bb06cc3
 
 
57de3e4
53fb129
bb06cc3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import io
import gradio as gr
from PIL import Image
import vtracer
import tempfile

# Localization dictionary
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."""
    # Convert Gradio image to bytes for vtracer compatibility
    img_byte_array = io.BytesIO()
    image.save(img_byte_array, format='PNG')
    img_bytes = img_byte_array.getvalue()

    # Perform the conversion
    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)
    )

    # Save the SVG string to a temporary file
    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
    )

# Gradio interface
vector_converter_interface = gr.Blocks()

with vector_converter_interface:
    language_dropdown = gr.Dropdown(
        choices=['en', 'de'], 
        value='en', 
        label='Language'
    )

    # FULL REWRITE START (1/2 allowed)
    title_component = gr.Markdown(TRANSLATIONS['en']['title'])
    description_component = gr.Markdown(TRANSLATIONS['en']['description'])
    
    with gr.Row():
        image_input = gr.Image(type="pil", label="Upload Image")
    with gr.Row():
        with gr.Column():
            color_mode = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
            hierarchical = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical")
            mode = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode")
            filter_speckle = gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle")
            color_precision = gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision")
        with gr.Column():
            layer_difference = gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference")
            corner_threshold = gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold")
            length_threshold = gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold")
            max_iterations = gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations")
            splice_threshold = gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold")
            path_precision = gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
    
    convert_btn = gr.Button("Convert")
    
    svg_output = gr.HTML(label="SVG Output")
    file_output = gr.File(label="Download SVG")
    
    convert_btn.click(
        convert_image,
        inputs=[
            image_input, color_mode, hierarchical, mode, filter_speckle,
            color_precision, layer_difference, corner_threshold,
            length_threshold, max_iterations, splice_threshold, path_precision, language_dropdown
        ],
        outputs=[svg_output, file_output]
    )
    # FULL REWRITE END

    # HALF-LINE CHANGE 1/2 (gr.Textbox → gr.Markdown)
    def update_language(language):
        return {
            title_component: TRANSLATIONS[language]['title'],
            description_component: TRANSLATIONS[language]['description']
        }

    # HALF-LINE CHANGE 2/2 (removed invisible textboxes)
    language_dropdown.change(
        update_language, 
        inputs=language_dropdown, 
        outputs=[title_component, description_component]
    )

vector_converter_interface.launch()