Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,17 +9,14 @@ def image_to_svg(image, colormode, hierarchical, filter_speckle, color_precision
|
|
| 9 |
This function is triggered automatically when any of the input controls change.
|
| 10 |
"""
|
| 11 |
if image is None:
|
| 12 |
-
# A clear startup state before an image is uploaded.
|
| 13 |
return None, "Upload an image to see the SVG preview and code.", None
|
| 14 |
|
| 15 |
input_path = image
|
| 16 |
|
| 17 |
-
# Create a persistent temporary file that Gradio can access after the function returns.
|
| 18 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".svg", mode='w', encoding='utf-8') as temp_output_file:
|
| 19 |
output_path = temp_output_file.name
|
| 20 |
|
| 21 |
try:
|
| 22 |
-
# vtracer conversion call with all parameters from the UI
|
| 23 |
vtracer.convert_image_to_svg_py(
|
| 24 |
input_path,
|
| 25 |
output_path,
|
|
@@ -33,18 +30,15 @@ def image_to_svg(image, colormode, hierarchical, filter_speckle, color_precision
|
|
| 33 |
length_threshold=float(length_threshold),
|
| 34 |
splice_threshold=int(splice_threshold),
|
| 35 |
path_precision=int(path_precision),
|
| 36 |
-
max_iterations=10
|
| 37 |
)
|
| 38 |
|
| 39 |
-
# Read the generated SVG content to display in the code block
|
| 40 |
with open(output_path, "r", encoding='utf-8') as f:
|
| 41 |
svg_content = f.read()
|
| 42 |
|
| 43 |
-
# Return the path for the file download, the SVG code, and the path for the image preview.
|
| 44 |
return output_path, svg_content, output_path
|
| 45 |
|
| 46 |
except Exception as e:
|
| 47 |
-
# Handle potential errors during conversion and display them
|
| 48 |
error_message = f"An error occurred during conversion: {str(e)}"
|
| 49 |
return None, error_message, None
|
| 50 |
|
|
@@ -60,12 +54,12 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 60 |
gr.Markdown(
|
| 61 |
"""
|
| 62 |
# IMG2SVG: Real-time SVG Converter
|
| 63 |
-
This application converts raster images
|
| 64 |
|
| 65 |
**How to use:**
|
| 66 |
1. Upload an image.
|
| 67 |
2. Adjust the settings in the control panel below.
|
| 68 |
-
3. The SVG preview
|
| 69 |
"""
|
| 70 |
)
|
| 71 |
|
|
@@ -103,20 +97,23 @@ with gr.Blocks(theme=gr.themes.Soft(primary_hue="blue")) as demo:
|
|
| 103 |
|
| 104 |
# --- Event Handling for Live Update ---
|
| 105 |
|
| 106 |
-
|
| 107 |
-
controls = [
|
| 108 |
image_input, colormode, hierarchical, filter_speckle, color_precision,
|
| 109 |
layer_difference, mode, corner_threshold, length_threshold, splice_threshold, path_precision
|
| 110 |
]
|
| 111 |
|
| 112 |
-
|
| 113 |
-
outputs = [svg_file_output, svg_text_output, svg_image_output]
|
| 114 |
|
| 115 |
-
#
|
| 116 |
-
#
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 120 |
|
| 121 |
# To launch the application
|
| 122 |
demo.launch()
|
|
|
|
| 9 |
This function is triggered automatically when any of the input controls change.
|
| 10 |
"""
|
| 11 |
if image is None:
|
|
|
|
| 12 |
return None, "Upload an image to see the SVG preview and code.", None
|
| 13 |
|
| 14 |
input_path = image
|
| 15 |
|
|
|
|
| 16 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".svg", mode='w', encoding='utf-8') as temp_output_file:
|
| 17 |
output_path = temp_output_file.name
|
| 18 |
|
| 19 |
try:
|
|
|
|
| 20 |
vtracer.convert_image_to_svg_py(
|
| 21 |
input_path,
|
| 22 |
output_path,
|
|
|
|
| 30 |
length_threshold=float(length_threshold),
|
| 31 |
splice_threshold=int(splice_threshold),
|
| 32 |
path_precision=int(path_precision),
|
| 33 |
+
max_iterations=10
|
| 34 |
)
|
| 35 |
|
|
|
|
| 36 |
with open(output_path, "r", encoding='utf-8') as f:
|
| 37 |
svg_content = f.read()
|
| 38 |
|
|
|
|
| 39 |
return output_path, svg_content, output_path
|
| 40 |
|
| 41 |
except Exception as e:
|
|
|
|
| 42 |
error_message = f"An error occurred during conversion: {str(e)}"
|
| 43 |
return None, error_message, None
|
| 44 |
|
|
|
|
| 54 |
gr.Markdown(
|
| 55 |
"""
|
| 56 |
# IMG2SVG: Real-time SVG Converter
|
| 57 |
+
This application converts raster images into clean, scalable vector graphics (SVG).
|
| 58 |
|
| 59 |
**How to use:**
|
| 60 |
1. Upload an image.
|
| 61 |
2. Adjust the settings in the control panel below.
|
| 62 |
+
3. The SVG preview updates automatically when you change a setting.
|
| 63 |
"""
|
| 64 |
)
|
| 65 |
|
|
|
|
| 97 |
|
| 98 |
# --- Event Handling for Live Update ---
|
| 99 |
|
| 100 |
+
all_inputs = [
|
|
|
|
| 101 |
image_input, colormode, hierarchical, filter_speckle, color_precision,
|
| 102 |
layer_difference, mode, corner_threshold, length_threshold, splice_threshold, path_precision
|
| 103 |
]
|
| 104 |
|
| 105 |
+
all_outputs = [svg_file_output, svg_text_output, svg_image_output]
|
|
|
|
| 106 |
|
| 107 |
+
# **THE FIX:** Use '.release' for sliders to avoid excessive updates.
|
| 108 |
+
# Use '.change' for controls that only fire once per action.
|
| 109 |
+
|
| 110 |
+
# Sliders will trigger the function only when the user lets go of the mouse.
|
| 111 |
+
for slider in [filter_speckle, color_precision, layer_difference, corner_threshold, length_threshold, splice_threshold, path_precision]:
|
| 112 |
+
slider.release(fn=image_to_svg, inputs=all_inputs, outputs=all_outputs)
|
| 113 |
+
|
| 114 |
+
# Image upload and radio buttons will trigger immediately on change.
|
| 115 |
+
for component in [image_input, colormode, hierarchical, mode]:
|
| 116 |
+
component.change(fn=image_to_svg, inputs=all_inputs, outputs=all_outputs)
|
| 117 |
|
| 118 |
# To launch the application
|
| 119 |
demo.launch()
|