File size: 1,285 Bytes
9f89a13
 
 
 
08ffb09
9f89a13
 
 
 
 
 
08ffb09
9f89a13
 
 
 
08ffb09
9f89a13
08ffb09
 
9f89a13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
08ffb09
 
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
import os
import tempfile
from PIL import Image
from pixels2svg import pixels2svg
import gradio as gr

def vectorize_png_to_svg(input_path: str) -> str:
    """
    Take a PNG filepath, vectorize it, and return SVG text.
    """
    with Image.open(input_path) as im:
        im = im.convert("RGBA")
        tmp_png = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
        im.save(tmp_png.name, "PNG")

    drawing = pixels2svg(tmp_png.name)
    svg_text = drawing.tostring()

    os.remove(tmp_png.name)
    return svg_text

def gradio_vectorize(image_path):
    if image_path is None:
        return None

    svg_text = vectorize_png_to_svg(image_path)

    tmp_svg = tempfile.NamedTemporaryFile(delete=False, suffix=".svg")
    with open(tmp_svg.name, "w", encoding="utf-8") as f:
        f.write(svg_text)

    return tmp_svg.name

with gr.Blocks() as demo:
    gr.Markdown("⥏⥑ 𓍴 -Vectorizer- 𓍴 ⥏⥑")
    gr.Markdown("PNG📷 ➲ SVG💾.")

    with gr.Row():
        in_img = gr.Image(type="filepath", label="📷PNG input📷")
        out_file = gr.File(label="💾SVG output💾")

    run_btn = gr.Button("♻️ Vectorize ♻️")
    run_btn.click(fn=gradio_vectorize, inputs=in_img, outputs=out_file)

if __name__ == "__main__":
    demo.launch()