""" Vectorizer Engine powered by VTracer (Rust-backed mathematical contour tracing) Converts raster bitmaps (PNG, JPG, WebP) into crisp, scalable SVG vectors. """ from pathlib import Path from typing import Optional, Dict, Any import vtracer class Vectorizer: # Presets tuned for different 2D visual styles PRESETS = { "character": { "colormode": "color", "hierarchical": "stacked", "mode": "spline", "filter_speckle": 4, # filters stray noise / compression artifacts "color_precision": 7, # higher fidelity for color boundaries "layer_difference": 16, # clean separation between color regions "corner_threshold": 60, # crisp anime corners "length_threshold": 4.0, # smooth curves "max_iterations": 10, "splice_threshold": 45, "path_precision": 3, }, "flat_icon": { "colormode": "color", "hierarchical": "stacked", "mode": "spline", "filter_speckle": 8, "color_precision": 6, "layer_difference": 24, "corner_threshold": 70, "length_threshold": 5.0, "max_iterations": 8, "splice_threshold": 50, "path_precision": 2, }, "detailed_lineart": { "colormode": "color", "hierarchical": "stacked", "mode": "spline", "filter_speckle": 2, "color_precision": 8, "layer_difference": 10, "corner_threshold": 50, "length_threshold": 3.0, "max_iterations": 15, "splice_threshold": 35, "path_precision": 4, }, "monochrome": { "colormode": "binary", "hierarchical": "stacked", "mode": "spline", "filter_speckle": 4, "color_precision": 5, "layer_difference": 16, "corner_threshold": 60, "length_threshold": 4.0, "max_iterations": 10, "splice_threshold": 45, "path_precision": 3, } } @classmethod def vectorize_file( cls, input_path: str | Path, output_path: str | Path, preset: str = "character", custom_params: Optional[Dict[str, Any]] = None, ) -> str: """ Converts an image file on disk to an SVG file. Returns the path to the generated SVG. """ params = dict(cls.PRESETS.get(preset, cls.PRESETS["character"])) if custom_params: params.update(custom_params) input_str = str(Path(input_path).resolve()) output_str = str(Path(output_path).resolve()) # Pass positionally for robust PyO3 / C-binding compatibility across Python versions vtracer.convert_image_to_svg_py( input_str, output_str, str(params.get("colormode", "color")), str(params.get("hierarchical", "stacked")), str(params.get("mode", "spline")), int(params.get("filter_speckle", 4)), int(params.get("color_precision", 7)), int(params.get("layer_difference", 16)), int(params.get("corner_threshold", 60)), float(params.get("length_threshold", 4.0)), int(params.get("max_iterations", 10)), int(params.get("splice_threshold", 45)), int(params.get("path_precision", 3)), ) return output_str