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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -33
app.py CHANGED
@@ -5,17 +5,19 @@ 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
- # Save image to temporary file, as vtracer 0.6.11 requires a file path
12
- with tempfile.NamedTemporaryFile(suffix='.png', delete=False) as temp_img:
13
- image.save(temp_img, format='PNG')
14
- temp_img_path = temp_img.name
15
 
16
- # Convert image to SVG using vtracer
17
- svg_str = vtracer.convert_image_to_svg(
18
- image_path=temp_img_path,
 
 
 
 
 
 
19
  colormode=color_mode.lower(),
20
  hierarchical=hierarchical.lower(),
21
  mode=mode.lower(),
@@ -29,38 +31,41 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
29
  path_precision=int(path_precision)
30
  )
31
 
32
- # Save SVG to temporary file for download
33
  temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
34
  temp_file.write(svg_str.encode('utf-8'))
35
  temp_file.close()
36
 
37
- return temp_file.name
38
 
39
  # Gradio interface
40
  iface = gr.Blocks()
41
 
42
  with iface:
 
43
  gr.Interface(
44
- fn=convert_image,
45
- inputs=[
46
- gr.Image(type="pil", label="Upload Image"),
47
- gr.Radio(choices=["color", "binary"], value="color", label="Color Mode"),
48
- gr.Radio(choices=["stacked", "cutout"], value="stacked", label="Hierarchical"),
49
- gr.Radio(choices=["spline", "polygon", "none"], value="spline", label="Mode"),
50
- gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"),
51
- gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"),
52
- gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"),
53
- gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"),
54
- gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
55
- gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
56
- gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
57
- gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
58
- ],
59
- outputs=[
60
- gr.File(label="Download SVG")
61
- ],
62
- title="Convert Image to SVG vectors",
63
- description="Upload an image and customize the conversion parameters as needed.<br><h2>Support me USDT (TRC-20): TAe7hsSVWtMEYz3G5V1UiUdYPQVqm28bKx</h2>",
64
- )
 
65
 
66
- iface.launch(ssr=False)
 
 
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(),
 
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()