AkashKumarave commited on
Commit
d09af21
·
verified ·
1 Parent(s): 37ffc92

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +186 -59
app.py CHANGED
@@ -1,26 +1,34 @@
1
- import io
2
  import gradio as gr
3
- from PIL import Image
4
  import vtracer
5
- import tempfile
6
-
7
- def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
8
- color_precision, layer_difference, corner_threshold,
9
- length_threshold, max_iterations, splice_threshold, path_precision):
10
- """Converts an image to SVG using vtracer with customizable parameters."""
11
-
12
- # Convert Gradio image to bytes for vtracer compatibility
13
- img_byte_array = io.BytesIO()
14
- image.save(img_byte_array, format='PNG')
15
- img_bytes = img_byte_array.getvalue()
16
-
17
- # Perform the conversion
18
- svg_str = vtracer.convert_raw_image_to_svg(
19
- img_bytes,
20
- img_format='png',
21
- colormode=color_mode.lower(),
22
- hierarchical=hierarchical.lower(),
23
- mode=mode.lower(),
 
 
 
 
 
 
 
 
 
 
24
  filter_speckle=int(filter_speckle),
25
  color_precision=int(color_precision),
26
  layer_difference=int(layer_difference),
@@ -30,42 +38,161 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
30
  splice_threshold=int(splice_threshold),
31
  path_precision=int(path_precision)
32
  )
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33
 
34
- # Save the SVG string to a temporary file
35
- temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
36
- temp_file.write(svg_str.encode('utf-8'))
37
- temp_file.close()
38
-
39
- return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), temp_file.name
40
-
41
- # Gradio interface
42
- iface = gr.Blocks()
43
-
44
- with iface:
45
-
46
- gr.Interface(
47
- fn=convert_image,
48
- inputs=[
49
- gr.Image(type="pil", label="Upload Image"),
50
- gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
51
- gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
52
- gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
53
- gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"),
54
- gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"),
55
- gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"),
56
- gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"),
57
- gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
58
- gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
59
- gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
60
- gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
61
- ],
62
- outputs=[
63
- gr.HTML(label="SVG Output"),
64
- gr.File(label="Download SVG")
65
- ],
66
- title="Convert Image to SVG vectors",
67
- description="Upload an image and customize the conversion parameters as needed.<br><h2>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2>",
68
- )
69
-
70
-
71
- iface.launch()
 
 
1
  import gradio as gr
 
2
  import vtracer
3
+ import os
4
+
5
+ def convert_to_vector(
6
+ image,
7
+ colormode="color",
8
+ hierarchical="stacked",
9
+ mode="spline",
10
+ filter_speckle=4,
11
+ color_precision=6,
12
+ layer_difference=16,
13
+ corner_threshold=60,
14
+ length_threshold=4.0,
15
+ max_iterations=10,
16
+ splice_threshold=45,
17
+ path_precision=3
18
+ ):
19
+ input_path = "temp_input.jpg"
20
+ output_path = "svg_output.svg"
21
+
22
+ # Save the input image to a temporary file
23
+ image.save(input_path)
24
+
25
+ # Convert the image to SVG using VTracer
26
+ vtracer.convert_image_to_svg_py(
27
+ input_path,
28
+ output_path,
29
+ colormode=colormode,
30
+ hierarchical=hierarchical,
31
+ mode=mode,
32
  filter_speckle=int(filter_speckle),
33
  color_precision=int(color_precision),
34
  layer_difference=int(layer_difference),
 
38
  splice_threshold=int(splice_threshold),
39
  path_precision=int(path_precision)
40
  )
41
+ # Read the SVG output
42
+ with open(output_path, "r") as f:
43
+ svg_content = f.read()
44
+
45
+ # Return the SVG file path instead of content
46
+ return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_content}</svg>'), output_path
47
+ # return output_path,output_path
48
+
49
+ def handle_color_mode(value):
50
+ # You can change this to display the selected value without any prefix
51
+ return value
52
+
53
+
54
+ examples = [
55
+ "examples/11.jpg",
56
+ "examples/02.jpg",
57
+ "examples/03.jpg",
58
+ ]
59
+
60
+ css = """
61
+ #col-container {
62
+ margin: 0 auto;
63
+ max-width: 960px;
64
+ }
65
+ .generate-btn {
66
+ background: linear-gradient(90deg, #4B79A1 0%, #283E51 100%) !important;
67
+ border: none !important;
68
+ color: white !important;
69
+ }
70
+ .generate-btn:hover {
71
+ transform: translateY(-2px);
72
+ box-shadow: 0 5px 15px rgba(0,0,0,0.2);
73
+ }
74
+ """
75
+
76
+ # Define the Gradio interface
77
+ with gr.Blocks(css=css) as app:
78
+ with gr.Column(elem_id="col-container"):
79
+ gr.HTML("""
80
+ <div style="text-align: center;">
81
+ <h2>Image to Vector Converter ⚡</h2>
82
+ <p>Converts raster images (JPG, PNG, WEBP) to vector graphics (SVG).</p>
83
+ </div>
84
+ """)
85
+ with gr.Row():
86
+ with gr.Column():
87
+ image_input = gr.Image(type="pil", label="Upload Image")
88
+ with gr.Accordion("Advanced Settings", open=False):
89
+ with gr.Accordion("Clustering", open=False):
90
+ colormode = gr.Radio([("COLOR","color"),("B/W", "binary")], value="color", label="Color Mode", show_label=False)
91
+ filter_speckle = gr.Slider(0, 128, value=4, step=1, label="Filter Speckle", info="Cleaner")
92
+ color_precision = gr.Slider(1, 8, value=6, step=1, label="Color Precision", info="More accurate")
93
+ layer_difference = gr.Slider(0, 128, value=16, step=1, label="Gradient Step", info="Less layers")
94
+ hierarchical = gr.Radio([("STACKED","stacked"), ("CUTOUT","cutout")], value="stacked", label="Hierarchical Mode",show_label=False)
95
+ with gr.Accordion("Curve Fitting", open=False):
96
+ mode = gr.Radio([("SPLINE","spline"),("POLYGON", "polygon"), ("PIXEL","none")], value="spline", label="Mode", show_label=False)
97
+ corner_threshold = gr.Slider(0, 180, value=60, step=1, label="Corner Threshold", info="Smoother")
98
+ length_threshold = gr.Slider(3.5, 10, value=4.0, step=0.1, label="Segment Length", info ="More coarse")
99
+ splice_threshold = gr.Slider(0, 180, value=45, step=1, label="Splice Threshold", info="Less accurate")
100
+ max_iterations = gr.Slider(1, 20, value=10, step=1, label="Max Iterations", visible=False)
101
+ path_precision = gr.Slider(1, 10, value=3, step=1, label="Path Precision", visible=False)
102
+ output_text = gr.Textbox(label="Selected Mode", visible=False)
103
+ with gr.Row():
104
+ clear_button = gr.Button("Clear")
105
+ convert_button = gr.Button("✨ Convert to SVG", variant='primary', elem_classes=["generate-btn"])
106
+
107
+ with gr.Column():
108
+ html = gr.HTML(label="SVG Output") # container=True, show_label=True
109
+ svg_output = gr.File(label="Download SVG")
110
+
111
+ gr.Examples(
112
+ examples = examples,
113
+ fn = convert_to_vector,
114
+ inputs = [image_input],
115
+ outputs = [html,svg_output],
116
+ cache_examples=False,
117
+ run_on_click = True
118
+ )
119
+ # Store default values for restoration
120
+ colormode.change(handle_color_mode, inputs=colormode,outputs=output_text)
121
+ hierarchical.change(handle_color_mode, inputs=hierarchical,outputs=output_text)
122
+ mode.change(handle_color_mode, inputs=mode,outputs=output_text)
123
+ default_values = {
124
+ "color_precision": 6,
125
+ "layer_difference": 16
126
+ }
127
+
128
+ def clear_inputs():
129
+ return gr.Image(value=None), gr.Radio(value="color"), gr.Radio(value="stacked"), gr.Radio(value="spline"), gr.Slider(value=4), gr.Slider(value=6), gr.Slider(value=16), gr.Slider(value=60), gr.Slider(value=4.0), gr.Slider(value=10), gr.Slider(value=45), gr.Slider(value=3)
130
+
131
+
132
+ def update_interactivity_and_visibility(colormode, color_precision_value, layer_difference_value):
133
+ is_color_mode = colormode == "color"
134
+ return (
135
+ gr.update(interactive=is_color_mode),
136
+ gr.update(interactive=is_color_mode),
137
+ gr.update(visible=is_color_mode) # Show/Hide Hierarchical Mode
138
+ )
139
+
140
+ colormode.change(
141
+ update_interactivity_and_visibility,
142
+ inputs=[colormode, color_precision, layer_difference],
143
+ outputs=[color_precision, layer_difference, hierarchical]
144
+ )
145
+
146
+ def update_interactivity_and_visibility_for_mode(mode):
147
+ is_spline_mode = mode == "spline"
148
+ return (
149
+ gr.update(interactive=is_spline_mode),
150
+ gr.update(interactive=is_spline_mode),
151
+ gr.update(interactive=is_spline_mode)
152
+ )
153
+
154
+ mode.change(
155
+ update_interactivity_and_visibility_for_mode,
156
+ inputs=[mode],
157
+ outputs=[corner_threshold,length_threshold,splice_threshold]
158
+ )
159
+
160
+ clear_button.click(
161
+ clear_inputs,
162
+ outputs=[
163
+ image_input,
164
+ colormode,
165
+ hierarchical,
166
+ mode,
167
+ filter_speckle,
168
+ color_precision,
169
+ layer_difference,
170
+ corner_threshold,
171
+ length_threshold,
172
+ max_iterations,
173
+ splice_threshold,
174
+ path_precision
175
+ ]
176
+ )
177
+
178
+ convert_button.click(
179
+ convert_to_vector,
180
+ inputs=[
181
+ image_input,
182
+ colormode,
183
+ hierarchical,
184
+ mode,
185
+ filter_speckle,
186
+ color_precision,
187
+ layer_difference,
188
+ corner_threshold,
189
+ length_threshold,
190
+ max_iterations,
191
+ splice_threshold,
192
+ path_precision
193
+ ],
194
+ outputs=[html,svg_output]
195
+ )
196
 
197
+ # Launch the app
198
+ app.launch(debug=True)