handicraft_editor / src /services /vectorizer.py
Deepika-05's picture
Update src/services/vectorizer.py
39a46b7 verified
Raw
History Blame Contribute Delete
926 Bytes
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