File size: 926 Bytes
a68d406
 
 
0420b4f
fae08fb
a68d406
0420b4f
 
 
 
a68d406
0420b4f
 
c57d349
0420b4f
fae08fb
da548e4
0420b4f
 
 
 
 
 
 
 
da548e4
fae08fb
 
 
 
 
0420b4f
 
fae08fb
 
 
 
 
 
 
acaebf4
0420b4f
 
 
 
f2fe759
0420b4f
f2fe759
fae08fb
 
0420b4f
fae08fb
 
0420b4f
fae08fb
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
import os
import time
import cv2

from vtracer import convert_image_to_svg_py

def generate_svg(
    edges,
    skeletonize
):
    """
    Convert edge image
    → SVG bytes
    """

    temp_file = "temp.png"

    svg_file = (
        f"vector_{int(time.time())}.svg"
    )

    cv2.imwrite(
        temp_file,
        edges
    )

    convert_image_to_svg_py(
        temp_file,
        svg_file,
        colormode="binary",
        mode="spline",
        filter_speckle=
        10 if not skeletonize else 5,
        color_precision=6,
        layer_difference=16,
        corner_threshold=65,
        length_threshold=3.0,
        max_iterations=20,
        splice_threshold=45
    )

    with open(
        svg_file,
        "rb"
    ) as f:

        svg_bytes = f.read()

    if os.path.exists(temp_file):
        os.remove(temp_file)

    if os.path.exists(svg_file):
        os.remove(svg_file)

    return svg_bytes