File size: 3,593 Bytes
a34a6d1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
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