Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -8,8 +8,9 @@ from skimage import color, filters, feature, morphology, exposure, util
|
|
8 |
import cv2
|
9 |
from scipy import ndimage
|
10 |
from sklearn.cluster import KMeans
|
|
|
11 |
|
12 |
-
def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization):
|
13 |
"""Advanced preprocessing of the image before vectorization."""
|
14 |
|
15 |
if blur_radius > 0:
|
@@ -42,26 +43,23 @@ def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail
|
|
42 |
|
43 |
if color_quantization > 0:
|
44 |
image = quantize_colors(image, color_quantization)
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
|
46 |
return image
|
47 |
|
48 |
-
def quantize_colors(image, n_colors):
|
49 |
-
"""Reduces the number of colors in the image using KMeans clustering."""
|
50 |
-
np_image = np.array(image)
|
51 |
-
h, w, c = np_image.shape
|
52 |
-
flat_image = np_image.reshape(-1, c)
|
53 |
-
kmeans = KMeans(n_clusters=n_colors, random_state=42).fit(flat_image)
|
54 |
-
labels = kmeans.predict(flat_image)
|
55 |
-
quantized_image = kmeans.cluster_centers_[labels].reshape(h, w, c).astype('uint8')
|
56 |
-
return Image.fromarray(quantized_image)
|
57 |
-
|
58 |
def convert_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization,
|
59 |
color_mode, hierarchical, mode, filter_speckle, color_precision, layer_difference,
|
60 |
-
corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision
|
|
|
61 |
"""Convert an image to SVG using vtracer with customizable and advanced parameters."""
|
62 |
|
63 |
# Preprocess the image with additional detail level settings
|
64 |
-
image = preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization)
|
65 |
|
66 |
# Convert Gradio image to bytes for vtracer compatibility
|
67 |
img_byte_array = io.BytesIO()
|
@@ -99,13 +97,13 @@ iface = gr.Blocks()
|
|
99 |
|
100 |
with iface:
|
101 |
gr.Markdown("# Super-Advanced Image to SVG Converter")
|
102 |
-
gr.Markdown("Upload an image and customize the conversion parameters for the highest quality vector results. This tool provides extensive options to analyze and vectorize images at a pixel level with advanced processing techniques.")
|
103 |
|
104 |
with gr.Row():
|
105 |
image_input = gr.Image(type="pil", label="Upload Image")
|
106 |
blur_radius_input = gr.Slider(minimum=0, maximum=10, value=0, step=0.1, label="Blur Radius (for smoothing)")
|
107 |
sharpen_radius_input = gr.Slider(minimum=0, maximum=5, value=0, step=0.1, label="Sharpen Radius")
|
108 |
noise_reduction_input = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="Noise Reduction")
|
|
|
109 |
|
110 |
with gr.Row():
|
111 |
detail_level_input = gr.Slider(minimum=0, maximum=10, value=5, step=1, label="Detail Level")
|
@@ -141,10 +139,9 @@ with iface:
|
|
141 |
image_input, blur_radius_input, sharpen_radius_input, noise_reduction_input, detail_level_input, edge_method_input, color_quantization_input,
|
142 |
color_mode_input, hierarchical_input, mode_input, filter_speckle_input, color_precision_input,
|
143 |
layer_difference_input, corner_threshold_input, length_threshold_input, max_iterations_input,
|
144 |
-
splice_threshold_input, path_precision_input
|
145 |
],
|
146 |
outputs=[svg_output, download_output]
|
147 |
)
|
148 |
|
149 |
-
|
150 |
iface.launch()
|
|
|
8 |
import cv2
|
9 |
from scipy import ndimage
|
10 |
from sklearn.cluster import KMeans
|
11 |
+
from rembg import remove
|
12 |
|
13 |
+
def preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai):
|
14 |
"""Advanced preprocessing of the image before vectorization."""
|
15 |
|
16 |
if blur_radius > 0:
|
|
|
43 |
|
44 |
if color_quantization > 0:
|
45 |
image = quantize_colors(image, color_quantization)
|
46 |
+
|
47 |
+
if enhance_with_ai:
|
48 |
+
image_np = np.array(image)
|
49 |
+
# AI-based enhancement with rembg for smoothing edges and improving vectorization
|
50 |
+
image_np = remove(image_np)
|
51 |
+
image = Image.fromarray(image_np)
|
52 |
|
53 |
return image
|
54 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
def convert_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization,
|
56 |
color_mode, hierarchical, mode, filter_speckle, color_precision, layer_difference,
|
57 |
+
corner_threshold, length_threshold, max_iterations, splice_threshold, path_precision,
|
58 |
+
enhance_with_ai):
|
59 |
"""Convert an image to SVG using vtracer with customizable and advanced parameters."""
|
60 |
|
61 |
# Preprocess the image with additional detail level settings
|
62 |
+
image = preprocess_image(image, blur_radius, sharpen_radius, noise_reduction, detail_level, edge_method, color_quantization, enhance_with_ai)
|
63 |
|
64 |
# Convert Gradio image to bytes for vtracer compatibility
|
65 |
img_byte_array = io.BytesIO()
|
|
|
97 |
|
98 |
with iface:
|
99 |
gr.Markdown("# Super-Advanced Image to SVG Converter")
|
|
|
100 |
|
101 |
with gr.Row():
|
102 |
image_input = gr.Image(type="pil", label="Upload Image")
|
103 |
blur_radius_input = gr.Slider(minimum=0, maximum=10, value=0, step=0.1, label="Blur Radius (for smoothing)")
|
104 |
sharpen_radius_input = gr.Slider(minimum=0, maximum=5, value=0, step=0.1, label="Sharpen Radius")
|
105 |
noise_reduction_input = gr.Slider(minimum=0, maximum=30, value=0, step=1, label="Noise Reduction")
|
106 |
+
enhance_with_ai_input = gr.Checkbox(label="AI Edge Enhance", value=False)
|
107 |
|
108 |
with gr.Row():
|
109 |
detail_level_input = gr.Slider(minimum=0, maximum=10, value=5, step=1, label="Detail Level")
|
|
|
139 |
image_input, blur_radius_input, sharpen_radius_input, noise_reduction_input, detail_level_input, edge_method_input, color_quantization_input,
|
140 |
color_mode_input, hierarchical_input, mode_input, filter_speckle_input, color_precision_input,
|
141 |
layer_difference_input, corner_threshold_input, length_threshold_input, max_iterations_input,
|
142 |
+
splice_threshold_input, path_precision_input, enhance_with_ai_input
|
143 |
],
|
144 |
outputs=[svg_output, download_output]
|
145 |
)
|
146 |
|
|
|
147 |
iface.launch()
|