Update app.py
Browse files
app.py
CHANGED
|
@@ -2,26 +2,22 @@ import os
|
|
| 2 |
import tempfile
|
| 3 |
from PIL import Image
|
| 4 |
from pixels2svg import pixels2svg
|
| 5 |
-
|
| 6 |
|
| 7 |
def vectorize_png_to_svg(input_path: str) -> str:
|
| 8 |
"""
|
| 9 |
Take a PNG filepath, vectorize it, and return SVG text.
|
| 10 |
"""
|
| 11 |
-
# Ensure image is PNG and load to normalize
|
| 12 |
with Image.open(input_path) as im:
|
| 13 |
-
im = im.convert("RGBA")
|
| 14 |
tmp_png = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 15 |
im.save(tmp_png.name, "PNG")
|
| 16 |
|
| 17 |
drawing = pixels2svg(tmp_png.name)
|
| 18 |
-
svg_text = drawing.tostring()
|
| 19 |
-
return svg_text # <- just return the string
|
| 20 |
-
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
import tempfile
|
| 25 |
|
| 26 |
def gradio_vectorize(image_path):
|
| 27 |
if image_path is None:
|
|
@@ -47,4 +43,5 @@ with gr.Blocks() as demo:
|
|
| 47 |
run_btn.click(fn=gradio_vectorize, inputs=in_img, outputs=out_file)
|
| 48 |
|
| 49 |
if __name__ == "__main__":
|
| 50 |
-
demo.launch(
|
|
|
|
|
|
| 2 |
import tempfile
|
| 3 |
from PIL import Image
|
| 4 |
from pixels2svg import pixels2svg
|
| 5 |
+
import gradio as gr
|
| 6 |
|
| 7 |
def vectorize_png_to_svg(input_path: str) -> str:
|
| 8 |
"""
|
| 9 |
Take a PNG filepath, vectorize it, and return SVG text.
|
| 10 |
"""
|
|
|
|
| 11 |
with Image.open(input_path) as im:
|
| 12 |
+
im = im.convert("RGBA")
|
| 13 |
tmp_png = tempfile.NamedTemporaryFile(delete=False, suffix=".png")
|
| 14 |
im.save(tmp_png.name, "PNG")
|
| 15 |
|
| 16 |
drawing = pixels2svg(tmp_png.name)
|
| 17 |
+
svg_text = drawing.tostring()
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
os.remove(tmp_png.name)
|
| 20 |
+
return svg_text
|
|
|
|
| 21 |
|
| 22 |
def gradio_vectorize(image_path):
|
| 23 |
if image_path is None:
|
|
|
|
| 43 |
run_btn.click(fn=gradio_vectorize, inputs=in_img, outputs=out_file)
|
| 44 |
|
| 45 |
if __name__ == "__main__":
|
| 46 |
+
demo.launch()
|
| 47 |
+
|