File size: 8,554 Bytes
bb16d68 4300714 bb16d68 |
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
"""
Any2SVG - Image to SVG Vectorization Tool
A Gradio 6 application that converts raster images to SVG vector graphics.
Supports MCP server integration for AI agent tool calling.
Purpose: Convert any raster image (PNG, JPG, WebP, etc.) to SVG format
Inputs: Image file, vectorization parameters
Outputs: SVG file path or SVG content
Examples: Logo vectorization, artwork conversion, icon generation
Edge cases: Very large images, transparent backgrounds, gradients
Errors: Invalid image format, file I/O errors
"""
import os
import sys
from pathlib import Path
import gradio as gr
from PIL import Image
# Add dna to path for imports
sys.path.insert(0, str(Path(__file__).parent))
from dna.atoms.vectorizer import (
VectorizerConfig,
image_to_svg_file,
image_to_svg_string,
validate_image,
VectorizationError,
InvalidImageError,
)
from dna.molecules.mcp_handler import get_output_directory, process_mcp_request
def convert_image_to_svg(
image: Image.Image,
color_mode: str = "color",
filter_speckle: int = 4,
color_precision: int = 6,
corner_threshold: int = 60,
path_precision: int = 3,
) -> tuple[str, str]:
"""
Convert a raster image to SVG vector graphics.
Args:
image: The input image to convert (PIL Image).
color_mode: Vectorization mode - 'color' for full color, 'binary' for black/white.
filter_speckle: Remove small artifacts (pixels). Higher = more filtering.
color_precision: Color quantization bits (1-8). Lower = fewer colors.
corner_threshold: Angle threshold for corner detection (degrees).
path_precision: Decimal precision for path coordinates.
Returns:
Tuple of (svg_file_path, svg_content) - path to saved SVG and its content.
Raises:
ValueError: If image is None or invalid.
IOError: If file operations fail.
"""
config = VectorizerConfig(
color_mode=color_mode,
filter_speckle=filter_speckle,
color_precision=color_precision,
corner_threshold=corner_threshold,
path_precision=path_precision,
)
output_dir = get_output_directory()
svg_path = image_to_svg_file(image, output_dir, config=config)
with open(svg_path, "r", encoding="utf-8") as f:
svg_content = f.read()
return str(svg_path), svg_content
def process_image(
image: Image.Image,
color_mode: str,
filter_speckle: int,
color_precision: int,
corner_threshold: int,
path_precision: int,
) -> tuple[str, str, str]:
"""
Process an image and convert it to SVG format.
Args:
image: The input raster image to vectorize.
color_mode: 'color' for full color output, 'binary' for black and white.
filter_speckle: Speckle filter size to remove small artifacts (1-100).
color_precision: Color precision bits for quantization (1-8).
corner_threshold: Corner detection angle threshold in degrees (0-180).
path_precision: Decimal precision for SVG path coordinates (1-8).
Returns:
Tuple of (status_message, svg_file_path, svg_preview_content).
"""
try:
svg_path, svg_content = convert_image_to_svg(
image=image,
color_mode=color_mode,
filter_speckle=filter_speckle,
color_precision=color_precision,
corner_threshold=corner_threshold,
path_precision=path_precision,
)
status = f"✅ SVG saved to: {svg_path}"
return status, svg_path, svg_content
except Exception as e:
error_msg = f"❌ Error: {str(e)}"
return error_msg, "", ""
# Build Gradio Interface
with gr.Blocks(
title="Any2SVG - Image to SVG Converter",
theme=gr.themes.Soft(),
) as demo:
gr.Markdown(
"""
# 🎨 Any2SVG - Image to SVG Converter
Convert raster images (PNG, JPG, WebP, etc.) to scalable vector graphics (SVG).
**Features:**
- High-quality vectorization using vtracer
- Configurable color modes and precision
- MCP server support for AI agent integration
"""
)
with gr.Row():
with gr.Column(scale=1):
input_image = gr.Image(
label="Input Image",
type="pil",
sources=["upload", "clipboard"],
)
with gr.Accordion("Vectorization Settings", open=False):
color_mode = gr.Radio(
choices=["color", "binary"],
value="color",
label="Color Mode",
info="'color' for full color, 'binary' for black/white",
)
filter_speckle = gr.Slider(
minimum=1,
maximum=100,
value=4,
step=1,
label="Filter Speckle",
info="Remove small artifacts (higher = more filtering)",
)
color_precision = gr.Slider(
minimum=1,
maximum=8,
value=6,
step=1,
label="Color Precision",
info="Color quantization bits (lower = fewer colors)",
)
corner_threshold = gr.Slider(
minimum=0,
maximum=180,
value=60,
step=1,
label="Corner Threshold",
info="Angle threshold for corner detection (degrees)",
)
path_precision = gr.Slider(
minimum=1,
maximum=8,
value=3,
step=1,
label="Path Precision",
info="Decimal precision for path coordinates",
)
convert_btn = gr.Button("🔄 Convert to SVG", variant="primary", size="lg")
with gr.Column(scale=1):
status_output = gr.Textbox(
label="Status",
interactive=False,
)
svg_file_output = gr.File(
label="Download SVG",
)
svg_preview = gr.Code(
label="SVG Preview",
language="html",
lines=15,
)
# Event handler
convert_btn.click(
fn=process_image,
inputs=[
input_image,
color_mode,
filter_speckle,
color_precision,
corner_threshold,
path_precision,
],
outputs=[status_output, svg_file_output, svg_preview],
)
# Examples
gr.Examples(
examples=[
["https://upload.wikimedia.org/wikipedia/commons/thumb/4/47/PNG_transparency_demonstration_1.png/280px-PNG_transparency_demonstration_1.png"],
],
inputs=[input_image],
label="Example Images",
)
# MCP-only tool for direct conversion
@gr.api(
inputs=[
gr.Image(type="pil"),
gr.Textbox(value="color"),
gr.Number(value=4),
gr.Number(value=6),
],
outputs=[gr.Textbox(), gr.Textbox()],
)
def image_to_svg(
image: Image.Image,
color_mode: str = "color",
filter_speckle: int = 4,
color_precision: int = 6,
) -> tuple[str, str]:
"""
Convert any raster image to SVG vector graphics.
This tool accepts an image and returns the path to the generated SVG file
along with the SVG content. The SVG is saved to the configured output directory.
Args:
image: The input raster image (PNG, JPG, WebP, etc.) to vectorize.
color_mode: Vectorization mode - 'color' for full color, 'binary' for B&W.
filter_speckle: Speckle filter size (1-100). Higher removes more artifacts.
color_precision: Color precision bits (1-8). Lower means fewer colors.
Returns:
Tuple of (svg_file_path, svg_content).
"""
svg_path, svg_content = convert_image_to_svg(
image=image,
color_mode=color_mode,
filter_speckle=int(filter_speckle),
color_precision=int(color_precision),
)
return svg_path, svg_content
if __name__ == "__main__":
# Ensure output directory exists
get_output_directory()
# Launch with MCP server enabled
demo.launch(
mcp_server=True,
server_name="0.0.0.0",
server_port=7860,
share=False,
)
|