aaurelions commited on
Commit
fe88f1f
·
verified ·
1 Parent(s): 8dffc5e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -37
app.py CHANGED
@@ -11,43 +11,44 @@ def image_to_svg(image, colormode, hierarchical, filter_speckle, color_precision
11
  if image is None:
12
  return None, "Upload an image and click 'Convert'.", None
13
 
14
- # The input 'image' from gr.Image(type='filepath') is the path string.
15
  input_path = image
16
 
17
- # Use a temporary directory for the output to ensure a clean file system
18
- with tempfile.TemporaryDirectory() as temp_dir:
19
- output_filename = f"{os.path.splitext(os.path.basename(input_path))[0]}.svg"
20
- output_path = os.path.join(temp_dir, output_filename)
 
21
 
22
- try:
23
- # vtracer conversion call with all parameters from the UI
24
- vtracer.convert_image_to_svg_py(
25
- input_path,
26
- output_path,
27
- colormode=colormode.lower(),
28
- hierarchical=hierarchical.lower(),
29
- mode=mode.lower(),
30
- filter_speckle=int(filter_speckle),
31
- color_precision=int(color_precision),
32
- layer_difference=int(layer_difference),
33
- corner_threshold=int(corner_threshold),
34
- length_threshold=float(length_threshold),
35
- splice_threshold=int(splice_threshold),
36
- path_precision=int(path_precision),
37
- max_iterations=10 # A reasonable default
38
- )
39
 
40
- # Read the generated SVG content to display in the code block
41
- with open(output_path, "r") as f:
42
- svg_content = f.read()
43
-
44
- # Return the path for file download, the SVG code, and the path for the image preview
45
- return output_path, svg_content, output_path
 
46
 
47
- except Exception as e:
48
- # Handle potential errors during conversion and display them
49
- error_message = f"An error occurred during conversion: {str(e)}"
50
- return None, error_message, None
51
 
52
 
53
  # --- Gradio User Interface ---
@@ -76,7 +77,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
76
  splice_threshold = gr.Slider(0, 180, value=45, step=1, label="Splice Threshold (Less accurate)")
77
  path_precision = gr.Slider(1, 8, value=3, step=1, label="Path Precision")
78
 
79
- # Add a dedicated button to trigger the conversion
80
  convert_button = gr.Button("Convert", variant="primary")
81
 
82
  # ----- Output Column -----
@@ -89,7 +89,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
89
 
90
  svg_file_output = gr.File(label="Download SVG")
91
 
92
- # ----- Documentation -----
93
  with gr.Accordion("How VTracer Works (Technical Details)", open=False):
94
  gr.Markdown(
95
  """
@@ -103,7 +102,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
103
  """
104
  )
105
 
106
- # List of all input controls
107
  inputs = [
108
  image_input, colormode, hierarchical, filter_speckle, color_precision,
109
  layer_difference, mode, corner_threshold, length_threshold, splice_threshold, path_precision
@@ -111,8 +109,6 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
111
 
112
  outputs = [svg_file_output, svg_text_output, svg_image_output]
113
 
114
- # **FIX:** Connect the button's 'click' event to the conversion function.
115
- # This is the stable and correct way to handle the event.
116
  convert_button.click(fn=image_to_svg, inputs=inputs, outputs=outputs)
117
 
118
  # To launch the application
 
11
  if image is None:
12
  return None, "Upload an image and click 'Convert'.", None
13
 
 
14
  input_path = image
15
 
16
+ # **FIX:** Create a named temporary file that will NOT be deleted automatically.
17
+ # We use delete=False to ensure the file persists after the function returns.
18
+ # Gradio will handle the cleanup of this file from its own cache.
19
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".svg", mode='w', encoding='utf-8') as temp_output_file:
20
+ output_path = temp_output_file.name
21
 
22
+ try:
23
+ # vtracer conversion call with all parameters from the UI
24
+ vtracer.convert_image_to_svg_py(
25
+ input_path,
26
+ output_path,
27
+ colormode=colormode.lower(),
28
+ hierarchical=hierarchical.lower(),
29
+ mode=mode.lower(),
30
+ filter_speckle=int(filter_speckle),
31
+ color_precision=int(color_precision),
32
+ layer_difference=int(layer_difference),
33
+ corner_threshold=int(corner_threshold),
34
+ length_threshold=float(length_threshold),
35
+ splice_threshold=int(splice_threshold),
36
+ path_precision=int(path_precision),
37
+ max_iterations=10 # A reasonable default
38
+ )
39
 
40
+ # Read the generated SVG content to display in the code block
41
+ with open(output_path, "r", encoding='utf-8') as f:
42
+ svg_content = f.read()
43
+
44
+ # Return the path for file download, the SVG code, and the path for the image preview.
45
+ # The file at output_path now exists and Gradio can access it.
46
+ return output_path, svg_content, output_path
47
 
48
+ except Exception as e:
49
+ # Handle potential errors during conversion and display them
50
+ error_message = f"An error occurred during conversion: {str(e)}"
51
+ return None, error_message, None
52
 
53
 
54
  # --- Gradio User Interface ---
 
77
  splice_threshold = gr.Slider(0, 180, value=45, step=1, label="Splice Threshold (Less accurate)")
78
  path_precision = gr.Slider(1, 8, value=3, step=1, label="Path Precision")
79
 
 
80
  convert_button = gr.Button("Convert", variant="primary")
81
 
82
  # ----- Output Column -----
 
89
 
90
  svg_file_output = gr.File(label="Download SVG")
91
 
 
92
  with gr.Accordion("How VTracer Works (Technical Details)", open=False):
93
  gr.Markdown(
94
  """
 
102
  """
103
  )
104
 
 
105
  inputs = [
106
  image_input, colormode, hierarchical, filter_speckle, color_precision,
107
  layer_difference, mode, corner_threshold, length_threshold, splice_threshold, path_precision
 
109
 
110
  outputs = [svg_file_output, svg_text_output, svg_image_output]
111
 
 
 
112
  convert_button.click(fn=image_to_svg, inputs=inputs, outputs=outputs)
113
 
114
  # To launch the application