Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -4,32 +4,41 @@ 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 |
-
|
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')
|
30 |
img_bytes = img_byte_array.getvalue()
|
31 |
|
32 |
-
# Perform the conversion
|
33 |
svg_str = vtracer.convert_raw_image_to_svg(
|
34 |
img_bytes,
|
35 |
img_format='png',
|
@@ -59,13 +68,14 @@ def convert_image(image, blur_radius, edge_enhance, color_mode, hierarchical, mo
|
|
59 |
iface = gr.Blocks()
|
60 |
|
61 |
with iface:
|
62 |
-
gr.Markdown("#
|
63 |
-
gr.Markdown("Upload an image and
|
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")
|
@@ -93,7 +103,7 @@ with iface:
|
|
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 |
],
|
|
|
4 |
import vtracer
|
5 |
import tempfile
|
6 |
import numpy as np
|
7 |
+
from skimage import color, filters, feature, io as sk_io, morphology
|
8 |
|
9 |
+
def preprocess_image(image, blur_radius, edge_enhance, edge_threshold):
|
10 |
+
"""Applies advanced 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 |
+
# Convert image to grayscale and apply edge detection
|
18 |
+
grayscale_image = image.convert('L')
|
19 |
+
edge_image = grayscale_image.filter(ImageFilter.FIND_EDGES)
|
20 |
+
|
21 |
+
# Binarize the edges based on the threshold
|
22 |
+
edge_array = np.array(edge_image)
|
23 |
+
binary_edge_array = edge_array > edge_threshold
|
24 |
+
binary_edge_image = Image.fromarray(binary_edge_array.astype('uint8') * 255)
|
25 |
+
|
26 |
+
return binary_edge_image
|
27 |
|
28 |
+
def convert_image(image, blur_radius, edge_enhance, edge_threshold, color_mode, hierarchical, mode, filter_speckle,
|
|
|
|
|
29 |
color_precision, layer_difference, corner_threshold, length_threshold,
|
30 |
max_iterations, splice_threshold, path_precision):
|
31 |
+
"""Converts an image to SVG using vtracer with customizable parameters, aiming for higher quality vector results."""
|
32 |
|
33 |
# Preprocess the image
|
34 |
+
image = preprocess_image(image, blur_radius, edge_enhance, edge_threshold)
|
35 |
|
36 |
# Convert Gradio image to bytes for vtracer compatibility
|
37 |
img_byte_array = io.BytesIO()
|
38 |
image.save(img_byte_array, format='PNG')
|
39 |
img_bytes = img_byte_array.getvalue()
|
40 |
|
41 |
+
# Perform the conversion with enhanced vector settings
|
42 |
svg_str = vtracer.convert_raw_image_to_svg(
|
43 |
img_bytes,
|
44 |
img_format='png',
|
|
|
68 |
iface = gr.Blocks()
|
69 |
|
70 |
with iface:
|
71 |
+
gr.Markdown("# Advanced Vector Image Converter")
|
72 |
+
gr.Markdown("Upload an image and use advanced settings to produce vector images with clear, software-like lines.")
|
73 |
|
74 |
with gr.Row():
|
75 |
image_input = gr.Image(type="pil", label="Upload Image")
|
76 |
blur_radius_input = gr.Slider(minimum=0, maximum=10, value=0, step=0.5, label="Blur Radius (for smoothing)")
|
77 |
edge_enhance_input = gr.Checkbox(value=False, label="Edge Enhance")
|
78 |
+
edge_threshold_input = gr.Slider(minimum=0, maximum=255, value=100, step=5, label="Edge Detection Threshold")
|
79 |
|
80 |
with gr.Row():
|
81 |
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
|
|
|
103 |
convert_button.click(
|
104 |
fn=convert_image,
|
105 |
inputs=[
|
106 |
+
image_input, blur_radius_input, edge_enhance_input, edge_threshold_input, color_mode_input, hierarchical_input, mode_input,
|
107 |
filter_speckle_input, color_precision_input, layer_difference_input, corner_threshold_input,
|
108 |
length_threshold_input, max_iterations_input, splice_threshold_input, path_precision_input
|
109 |
],
|