AkashKumarave commited on
Commit
add4c79
·
verified ·
1 Parent(s): 5ee3801

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +71 -0
app.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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),
27
+ corner_threshold=int(corner_threshold),
28
+ length_threshold=float(length_threshold),
29
+ max_iterations=int(max_iterations),
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()