| import os |
| import tempfile |
| from PIL import Image |
| from pixels2svg import pixels2svg |
|
|
|
|
| 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() |
| return svg_text |
|
|
|
|
| import gradio as gr |
| import pathlib |
| import tempfile |
|
|
| 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(debug=True) |