|
|
import os |
|
|
os.system("pip uninstall -y gradio") |
|
|
os.system("pip install gradio>=4.44.1") |
|
|
import io |
|
|
import gradio as gr |
|
|
from PIL import Image |
|
|
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 with customizable parameters.""" |
|
|
|
|
|
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, |
|
|
|
|
|
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', mode='w', encoding='utf-8') |
|
|
temp_file.write(svg_str) |
|
|
temp_file.close() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
html_output = gr.HTML(svg_str) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return html_output, temp_file.name |
|
|
|
|
|
|
|
|
iface = gr.Blocks() |
|
|
|
|
|
with iface: |
|
|
gr.Markdown("# Hepzeka.com - Resmi SVG ye Dönüştür") |
|
|
gr.Markdown("Bir resim yükleyin ve dönüştürme parametrelerini ihtiyacınıza göre özelleştirin.") |
|
|
|
|
|
with gr.Row(): |
|
|
with gr.Column(scale=1): |
|
|
image_input = gr.Image(type="pil", label="Upload Image") |
|
|
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode") |
|
|
hierarchical_input = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical") |
|
|
mode_input = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode") |
|
|
filter_speckle_input = gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle") |
|
|
color_precision_input = gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision") |
|
|
layer_difference_input = gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference") |
|
|
corner_threshold_input = gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold") |
|
|
length_threshold_input = gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold") |
|
|
max_iterations_input = gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations") |
|
|
splice_threshold_input = gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold") |
|
|
path_precision_input = gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision") |
|
|
|
|
|
submit_button = gr.Button("Convert to SVG") |
|
|
|
|
|
with gr.Column(scale=1): |
|
|
svg_output_display = gr.HTML(label="SVG Output") |
|
|
svg_download_link = gr.File(label="Download SVG") |
|
|
|
|
|
inputs_list = [ |
|
|
image_input, |
|
|
color_mode_input, |
|
|
hierarchical_input, |
|
|
mode_input, |
|
|
filter_speckle_input, |
|
|
color_precision_input, |
|
|
layer_difference_input, |
|
|
corner_threshold_input, |
|
|
length_threshold_input, |
|
|
max_iterations_input, |
|
|
splice_threshold_input, |
|
|
path_precision_input |
|
|
] |
|
|
outputs_list = [ |
|
|
svg_output_display, |
|
|
svg_download_link |
|
|
] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
submit_button.click( |
|
|
fn=convert_image, |
|
|
inputs=inputs_list, |
|
|
outputs=outputs_list |
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
iface.launch() |
|
|
|