AkashKumarave commited on
Commit
eefdd50
·
verified ·
1 Parent(s): 7898a00

Update app.py

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