File size: 3,774 Bytes
905b102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ecd6431
 
 
905b102
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import io
import gradio as gr
import vtracer
import tempfile

def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
                  color_precision, layer_difference, corner_threshold,
                  length_threshold, max_iterations, splice_threshold, path_precision):
    """Converts an image to SVG using vtracer optimized for logos."""
    if image is None:
        return None, None

    # Image to bytes
    img_byte_array = io.BytesIO()
    image.save(img_byte_array, format='PNG')
    img_bytes = img_byte_array.getvalue()

    # VTracer 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 to temp file for download
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
    temp_file.write(svg_str.encode('utf-8'))
    temp_file.close()

    # Preview with responsive container
    svg_preview = f'<div style="background-color: white; padding: 10px; border-radius: 5px;">{svg_str}</div>'
    
    return svg_preview, temp_file.name

# Simple and clean UI
with gr.Blocks(title="Logo & Image Vectorizer") as demo:
    gr.Markdown("# 🚀 Logo & Image Vectorizer")
    gr.Markdown("Convert your PNG/JPG logos/images into scalable SVG vectors. Optimized for high-precision paths.")
    
    with gr.Row():
        with gr.Column():
            img_input = gr.Image(type="pil", label="Upload Logo (PNG/JPG)")
            
            with gr.Accordion("Fine-Tuning (Logo Optimized)", open=True):
                with gr.Row():
                    color_mode = gr.Radio(["Color", "Binary"], value="Color", label="Color Mode")
                    hierarchical = gr.Radio(["Stacked", "Cutout"], value="Stacked", label="Hierarchy")
                
                mode = gr.Radio(["Spline", "Polygon", "None"], value="Spline", label="Vector Mode")
                
                # Optimized defaults for Logos
                filter_speckle = gr.Slider(1, 10, value=2, step=1, label="Filter Speckle (Noise Reduction)")
                color_precision = gr.Slider(1, 8, value=8, step=1, label="Color Precision (Max 8)")
                layer_difference = gr.Slider(1, 32, value=16, step=1, label="Layer Difference")
                corner_threshold = gr.Slider(10, 90, value=60, step=1, label="Corner Threshold")
                length_threshold = gr.Slider(3.5, 10, value=4.0, step=0.5, label="Min Path Length")
                max_iterations = gr.Slider(1, 20, value=10, step=1, label="Curve Fitting Iterations")
                splice_threshold = gr.Slider(10, 90, value=45, step=1, label="Splice Threshold")
                path_precision = gr.Slider(1, 10, value=10, step=1, label="Path Precision (Decimal Places)")
            
            btn = gr.Button("Vectorize Now", variant="primary")

        with gr.Column():
            svg_output = gr.HTML(label="SVG Preview")
            file_output = gr.File(label="Download SVG")

    btn.click(
        fn=convert_image,
        inputs=[
            img_input, color_mode, hierarchical, mode, filter_speckle,
            color_precision, layer_difference, corner_threshold,
            length_threshold, max_iterations, splice_threshold, path_precision
        ],
        outputs=[svg_output, file_output]
    )

demo.launch()