| import io |
| import gradio as gr |
| import vtracer |
| import tempfile |
|
|
| def convert_image(image, color_mode, hierarchical, mode, filter_speckle, |
| color_precision, layer_difference, corner_threshold, |
| length_threshold, max_iterations, splice_threshold, path_precision): |
| """Converts an image to SVG using vtracer optimized for logos.""" |
| if image is None: |
| return None, None |
|
|
| |
| img_byte_array = io.BytesIO() |
| image.save(img_byte_array, format='PNG') |
| img_bytes = img_byte_array.getvalue() |
|
|
| |
| svg_str = vtracer.convert_raw_image_to_svg( |
| img_bytes, |
| img_format='png', |
| colormode=color_mode.lower(), |
| hierarchical=hierarchical.lower(), |
| mode=mode.lower(), |
| filter_speckle=int(filter_speckle), |
| color_precision=int(color_precision), |
| layer_difference=int(layer_difference), |
| corner_threshold=int(corner_threshold), |
| length_threshold=float(length_threshold), |
| max_iterations=int(max_iterations), |
| splice_threshold=int(splice_threshold), |
| path_precision=int(path_precision) |
| ) |
|
|
| |
| temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg') |
| temp_file.write(svg_str.encode('utf-8')) |
| temp_file.close() |
|
|
| |
| svg_preview = f'<div style="background-color: white; padding: 10px; border-radius: 5px;">{svg_str}</div>' |
| |
| return svg_preview, temp_file.name |
|
|
| |
| with gr.Blocks(title="Logo & Image Vectorizer") as demo: |
| gr.Markdown("# ๐ Logo & Image Vectorizer") |
| gr.Markdown("Convert your PNG/JPG logos/images into scalable SVG vectors. Optimized for high-precision paths.") |
| |
| with gr.Row(): |
| with gr.Column(): |
| img_input = gr.Image(type="pil", label="Upload Logo (PNG/JPG)") |
| |
| with gr.Accordion("Fine-Tuning (Logo Optimized)", open=True): |
| with gr.Row(): |
| color_mode = gr.Radio(["Color", "Binary"], value="Color", label="Color Mode") |
| hierarchical = gr.Radio(["Stacked", "Cutout"], value="Stacked", label="Hierarchy") |
| |
| mode = gr.Radio(["Spline", "Polygon", "None"], value="Spline", label="Vector Mode") |
| |
| |
| filter_speckle = gr.Slider(1, 10, value=2, step=1, label="Filter Speckle (Noise Reduction)") |
| color_precision = gr.Slider(1, 8, value=8, step=1, label="Color Precision (Max 8)") |
| layer_difference = gr.Slider(1, 32, value=16, step=1, label="Layer Difference") |
| corner_threshold = gr.Slider(10, 90, value=60, step=1, label="Corner Threshold") |
| length_threshold = gr.Slider(3.5, 10, value=4.0, step=0.5, label="Min Path Length") |
| max_iterations = gr.Slider(1, 20, value=10, step=1, label="Curve Fitting Iterations") |
| splice_threshold = gr.Slider(10, 90, value=45, step=1, label="Splice Threshold") |
| path_precision = gr.Slider(1, 10, value=10, step=1, label="Path Precision (Decimal Places)") |
| |
| btn = gr.Button("Vectorize Now", variant="primary") |
|
|
| with gr.Column(): |
| svg_output = gr.HTML(label="SVG Preview") |
| file_output = gr.File(label="Download SVG") |
|
|
| btn.click( |
| fn=convert_image, |
| inputs=[ |
| img_input, color_mode, hierarchical, mode, filter_speckle, |
| color_precision, layer_difference, corner_threshold, |
| length_threshold, max_iterations, splice_threshold, path_precision |
| ], |
| outputs=[svg_output, file_output] |
| ) |
|
|
| demo.launch() |