Spaces:
Sleeping
Sleeping
| import gradio as gr | |
| from PIL import Image | |
| import numpy as np | |
| import cv2 | |
| import tempfile | |
| import os | |
| import uuid | |
| # Persistent temp folder | |
| TEMP_DIR = "/tmp/vector_outputs" | |
| os.makedirs(TEMP_DIR, exist_ok=True) | |
| def image_to_svg(input_image): | |
| # Unique filenames (avoid conflicts) | |
| uid = str(uuid.uuid4()) | |
| png_path = os.path.join(TEMP_DIR, f"{uid}.png") | |
| svg_path = os.path.join(TEMP_DIR, f"{uid}.svg") | |
| # Save PNG | |
| input_image.save(png_path) | |
| # OpenCV processing | |
| img = cv2.imread(png_path) | |
| gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) | |
| _, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY) | |
| contours, _ = cv2.findContours( | |
| thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE | |
| ) | |
| height, width = gray.shape | |
| # Create SVG | |
| with open(svg_path, "w") as f: | |
| f.write(f'<svg xmlns="http://www.w3.org/2000/svg" width="{width}" height="{height}">') | |
| for cnt in contours: | |
| points = " ".join([f"{p[0][0]},{p[0][1]}" for p in cnt]) | |
| f.write(f'<polygon points="{points}" style="fill:black;stroke:none;" />') | |
| f.write("</svg>") | |
| return png_path, svg_path | |
| def process(image): | |
| return image_to_svg(image) | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# 🖼️ Image → Vector Converter (PNG + SVG)") | |
| input_image = gr.Image(type="pil", label="Upload Image") | |
| png_output = gr.File(label="Download PNG") | |
| svg_output = gr.File(label="Download SVG") | |
| btn = gr.Button("Convert") | |
| btn.click( | |
| fn=process, | |
| inputs=input_image, | |
| outputs=[png_output, svg_output] | |
| ) | |
| demo.launch() |