Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,29 @@
|
|
1 |
import io
|
2 |
import gradio as gr
|
3 |
-
from PIL import Image
|
4 |
import vtracer
|
5 |
import tempfile
|
|
|
|
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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')
|
@@ -44,11 +59,15 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
|
44 |
iface = gr.Blocks()
|
45 |
|
46 |
with iface:
|
47 |
-
gr.Markdown("# Convert Image to SVG Vectors")
|
48 |
-
gr.Markdown("Upload an image and customize the conversion parameters for
|
49 |
|
50 |
with gr.Row():
|
51 |
image_input = gr.Image(type="pil", label="Upload Image")
|
|
|
|
|
|
|
|
|
52 |
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
|
53 |
hierarchical_input = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical")
|
54 |
mode_input = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode")
|
@@ -74,10 +93,9 @@ with iface:
|
|
74 |
convert_button.click(
|
75 |
fn=convert_image,
|
76 |
inputs=[
|
77 |
-
image_input, color_mode_input, hierarchical_input, mode_input,
|
78 |
-
filter_speckle_input, color_precision_input, layer_difference_input,
|
79 |
-
|
80 |
-
splice_threshold_input, path_precision_input
|
81 |
],
|
82 |
outputs=[svg_output, download_output]
|
83 |
)
|
|
|
1 |
import io
|
2 |
import gradio as gr
|
3 |
+
from PIL import Image, ImageFilter
|
4 |
import vtracer
|
5 |
import tempfile
|
6 |
+
import numpy as np
|
7 |
+
from skimage import color, filters, feature, io as sk_io
|
8 |
|
9 |
+
def preprocess_image(image, blur_radius, edge_enhance):
|
10 |
+
"""Applies preprocessing steps to the image before tracing."""
|
11 |
+
if blur_radius > 0:
|
12 |
+
image = image.filter(ImageFilter.GaussianBlur(blur_radius))
|
13 |
+
|
14 |
+
if edge_enhance:
|
15 |
+
image = image.filter(ImageFilter.EDGE_ENHANCE_MORE)
|
16 |
+
|
17 |
+
return image
|
18 |
+
|
19 |
+
def convert_image(image, blur_radius, edge_enhance, color_mode, hierarchical, mode, filter_speckle,
|
20 |
+
color_precision, layer_difference, corner_threshold, length_threshold,
|
21 |
+
max_iterations, splice_threshold, path_precision):
|
22 |
"""Converts an image to SVG using vtracer with customizable parameters."""
|
23 |
|
24 |
+
# Preprocess the image
|
25 |
+
image = preprocess_image(image, blur_radius, edge_enhance)
|
26 |
+
|
27 |
# Convert Gradio image to bytes for vtracer compatibility
|
28 |
img_byte_array = io.BytesIO()
|
29 |
image.save(img_byte_array, format='PNG')
|
|
|
59 |
iface = gr.Blocks()
|
60 |
|
61 |
with iface:
|
62 |
+
gr.Markdown("# Enhanced Convert Image to SVG Vectors")
|
63 |
+
gr.Markdown("Upload an image and customize the advanced conversion parameters for high-quality, detailed vector results.")
|
64 |
|
65 |
with gr.Row():
|
66 |
image_input = gr.Image(type="pil", label="Upload Image")
|
67 |
+
blur_radius_input = gr.Slider(minimum=0, maximum=10, value=0, step=0.5, label="Blur Radius (for smoothing)")
|
68 |
+
edge_enhance_input = gr.Checkbox(value=False, label="Edge Enhance")
|
69 |
+
|
70 |
+
with gr.Row():
|
71 |
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
|
72 |
hierarchical_input = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical")
|
73 |
mode_input = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode")
|
|
|
93 |
convert_button.click(
|
94 |
fn=convert_image,
|
95 |
inputs=[
|
96 |
+
image_input, blur_radius_input, edge_enhance_input, color_mode_input, hierarchical_input, mode_input,
|
97 |
+
filter_speckle_input, color_precision_input, layer_difference_input, corner_threshold_input,
|
98 |
+
length_threshold_input, max_iterations_input, splice_threshold_input, path_precision_input
|
|
|
99 |
],
|
100 |
outputs=[svg_output, download_output]
|
101 |
)
|