aaurelions commited on
Commit
9e835dd
·
verified ·
1 Parent(s): eb8492b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +99 -0
app.py ADDED
@@ -0,0 +1,99 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import vtracer
3
+ import os
4
+ import tempfile
5
+
6
+ def image_to_svg(image, colormode, hierarchical, mode, filter_speckle, color_precision, layer_difference, corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision):
7
+ """
8
+ Converts an input image to an SVG string using vtracer.
9
+ """
10
+ if image is None:
11
+ return None, None
12
+
13
+ # Create a temporary directory to store the output file
14
+ with tempfile.TemporaryDirectory() as temp_dir:
15
+ # Use the uploaded file's original name for the output, but with an .svg extension
16
+ base, _ = os.path.splitext(os.path.basename(image.name))
17
+ output_filename = f"{base}.svg"
18
+ output_path = os.path.join(temp_dir, output_filename)
19
+
20
+ # Convert the image with the specified parameters
21
+ vtracer.convert_image_to_svg_py(
22
+ image.name,
23
+ output_path,
24
+ colormode=colormode,
25
+ hierarchical=hierarchical,
26
+ mode=mode,
27
+ filter_speckle=int(filter_speckle),
28
+ color_precision=int(color_precision),
29
+ layer_difference=int(layer_difference),
30
+ corner_threshold=int(corner_threshold),
31
+ length_threshold=float(length_threshold),
32
+ max_iterations=int(max_iterations),
33
+ splice_threshold=int(splice_threshold),
34
+ path_precision=int(path_precision)
35
+ )
36
+
37
+ # Read the content of the generated SVG file
38
+ with open(output_path, "r") as f:
39
+ svg_content = f.read()
40
+
41
+ return output_path, svg_content
42
+
43
+ # --- Gradio User Interface ---
44
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
45
+ gr.Markdown("# IMG2SVG: Image to SVG Converter")
46
+ gr.Markdown("Upload your JPG, PNG, or other image file to convert it to a scalable vector graphic (SVG). Adjust the settings below for finer control over the conversion process.")
47
+
48
+ with gr.Row():
49
+ with gr.Column(scale=1):
50
+ image_input = gr.Image(type="filepath", label="Upload Image")
51
+
52
+ with gr.Accordion("Conversion Settings", open=True):
53
+ colormode = gr.Dropdown(["color", "binary"], value="color", label="Color Mode")
54
+ hierarchical = gr.Dropdown(["stacked", "cutout"], value="stacked", label="Hierarchical")
55
+ mode = gr.Dropdown(["spline", "polygon", "none"], value="spline", label="Mode")
56
+ filter_speckle = gr.Slider(0, 16, value=4, step=1, label="Filter Speckle")
57
+ color_precision = gr.Slider(0, 8, value=6, step=1, label="Color Precision")
58
+ layer_difference = gr.Slider(0, 32, value=16, step=1, label="Layer Difference")
59
+ corner_threshold = gr.Slider(0, 180, value=60, step=1, label="Corner Threshold")
60
+ length_threshold = gr.Slider(0.0, 10.0, value=4.0, step=0.5, label="Length Threshold")
61
+ max_iterations = gr.Slider(1, 20, value=10, step=1, label="Max Iterations")
62
+ splice_threshold = gr.Slider(0, 90, value=45, step=1, label="Splice Threshold")
63
+ path_precision = gr.Slider(1, 8, value=3, step=1, label="Path Precision")
64
+
65
+ convert_button = gr.Button("Convert to SVG", variant="primary")
66
+
67
+ with gr.Column(scale=2):
68
+ svg_file_output = gr.File(label="Download SVG")
69
+ svg_text_output = gr.Code(label="SVG Code", language="xml")
70
+
71
+ # Connect the button to the conversion function
72
+ convert_button.click(
73
+ fn=image_to_svg,
74
+ inputs=[
75
+ image_input,
76
+ colormode,
77
+ hierarchical,
78
+ mode,
79
+ filter_speckle,
80
+ color_precision,
81
+ layer_difference,
82
+ corner_threshold,
83
+ length_threshold,
84
+ max_iterations,
85
+ splice_threshold,
86
+ path_precision
87
+ ],
88
+ outputs=[svg_file_output, svg_text_output]
89
+ )
90
+
91
+ with gr.Accordion("About vtracer", open=False):
92
+ gr.Markdown(
93
+ "This application uses the `vtracer` library to convert raster images into vector graphics. [1] "
94
+ "It offers a powerful alternative to tools like Adobe Illustrator's Image Trace, with a focus on creating compact SVG files. [1] "
95
+ "The core of `vtracer` is built in Rust for high performance. [1]"
96
+ )
97
+
98
+ # To launch the application
99
+ demo.launch()