Update app0.py
Browse files
app0.py
CHANGED
|
@@ -9,15 +9,22 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
|
| 9 |
length_threshold, max_iterations, splice_threshold, path_precision):
|
| 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')
|
| 15 |
img_bytes = img_byte_array.getvalue()
|
| 16 |
|
| 17 |
# Perform the conversion
|
| 18 |
svg_str = vtracer.convert_raw_image_to_svg(
|
| 19 |
img_bytes,
|
| 20 |
-
img_format='png',
|
| 21 |
colormode=color_mode.lower(),
|
| 22 |
hierarchical=hierarchical.lower(),
|
| 23 |
mode=mode.lower(),
|
|
@@ -32,40 +39,95 @@ def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
|
| 32 |
)
|
| 33 |
|
| 34 |
# Save the SVG string to a temporary file
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
-
return
|
| 40 |
|
| 41 |
# Gradio interface
|
| 42 |
iface = gr.Blocks()
|
| 43 |
|
| 44 |
with iface:
|
| 45 |
-
|
| 46 |
-
gr.
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
gr.
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
gr.File(label="Download SVG")
|
| 65 |
-
],
|
| 66 |
-
title="Hepzeka.com - Resmi SVG ye Dönüştür",
|
| 67 |
-
description="Bir resim yükleyin ve dönüştürme parametrelerini ihtiyacınıza göre özelleştirin.",
|
| 68 |
-
)
|
| 69 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 70 |
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
length_threshold, max_iterations, splice_threshold, path_precision):
|
| 10 |
"""Converts an image to SVG using vtracer with customizable parameters."""
|
| 11 |
|
| 12 |
+
if image is None:
|
| 13 |
+
# Handle case where no image is uploaded yet, or it's cleared.
|
| 14 |
+
# You might want to return empty/default outputs or raise a Gradio error.
|
| 15 |
+
# For simplicity, returning None which might clear the outputs or show placeholders.
|
| 16 |
+
# Depending on Gradio version, this might need more graceful handling.
|
| 17 |
+
return None, None
|
| 18 |
+
|
| 19 |
# Convert Gradio image to bytes for vtracer compatibility
|
| 20 |
img_byte_array = io.BytesIO()
|
| 21 |
+
image.save(img_byte_array, format='PNG') # Ensure input image is PIL, format can be inferred by PIL
|
| 22 |
img_bytes = img_byte_array.getvalue()
|
| 23 |
|
| 24 |
# Perform the conversion
|
| 25 |
svg_str = vtracer.convert_raw_image_to_svg(
|
| 26 |
img_bytes,
|
| 27 |
+
# img_format='png', # vtracer can often infer this, but specifying is safer
|
| 28 |
colormode=color_mode.lower(),
|
| 29 |
hierarchical=hierarchical.lower(),
|
| 30 |
mode=mode.lower(),
|
|
|
|
| 39 |
)
|
| 40 |
|
| 41 |
# Save the SVG string to a temporary file
|
| 42 |
+
# It's crucial that this temp file is not deleted immediately
|
| 43 |
+
# so Gradio's File component can serve it.
|
| 44 |
+
# NamedTemporaryFile needs to be kept open or its name stored carefully.
|
| 45 |
+
# A simpler approach for HF Spaces might be to write to a known path if persistence across calls isn't an issue,
|
| 46 |
+
# but NamedTemporaryFile with delete=False is generally fine.
|
| 47 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg', mode='w', encoding='utf-8')
|
| 48 |
+
temp_file.write(svg_str)
|
| 49 |
+
temp_file.close() # Close the file handle, but the file itself remains due to delete=False
|
| 50 |
+
|
| 51 |
+
# For gr.HTML, it's often better to pass the full SVG string.
|
| 52 |
+
# vtracer usually produces a full SVG document including <svg> tags.
|
| 53 |
+
# If image is None initially, image.width and image.height would cause an error.
|
| 54 |
+
# The viewBox construction you had is fine if svg_str is just the inner content.
|
| 55 |
+
# If svg_str is a full SVG, just use gr.HTML(svg_str).
|
| 56 |
+
# Assuming svg_str is a full SVG document:
|
| 57 |
+
html_output = gr.HTML(svg_str)
|
| 58 |
+
# If you want to ensure a specific viewBox based on original image for the HTML display:
|
| 59 |
+
# html_output = gr.HTML(f'<div style="width:{image.width}px; height:{image.height}px; overflow:auto;"><svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg></div>')
|
| 60 |
+
# However, simply `gr.HTML(svg_str)` is usually sufficient if vtracer provides proper width/height in the SVG.
|
| 61 |
|
| 62 |
+
return html_output, temp_file.name
|
| 63 |
|
| 64 |
# Gradio interface
|
| 65 |
iface = gr.Blocks()
|
| 66 |
|
| 67 |
with iface:
|
| 68 |
+
gr.Markdown("# Hepzeka.com - Resmi SVG ye Dönüştür") # Using Markdown for title
|
| 69 |
+
gr.Markdown("Bir resim yükleyin ve dönüştürme parametrelerini ihtiyacınıza göre özelleştirin.") # Using Markdown for description
|
| 70 |
+
|
| 71 |
+
with gr.Row():
|
| 72 |
+
with gr.Column(scale=1):
|
| 73 |
+
image_input = gr.Image(type="pil", label="Upload Image")
|
| 74 |
+
color_mode_input = gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode")
|
| 75 |
+
hierarchical_input = gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical")
|
| 76 |
+
mode_input = gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode")
|
| 77 |
+
filter_speckle_input = gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle")
|
| 78 |
+
color_precision_input = gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision")
|
| 79 |
+
layer_difference_input = gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference")
|
| 80 |
+
corner_threshold_input = gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold")
|
| 81 |
+
length_threshold_input = gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold")
|
| 82 |
+
max_iterations_input = gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations")
|
| 83 |
+
splice_threshold_input = gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold")
|
| 84 |
+
path_precision_input = gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
|
| 85 |
+
|
| 86 |
+
submit_button = gr.Button("Convert to SVG")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
+
with gr.Column(scale=1):
|
| 89 |
+
svg_output_display = gr.HTML(label="SVG Output")
|
| 90 |
+
svg_download_link = gr.File(label="Download SVG")
|
| 91 |
+
|
| 92 |
+
inputs_list = [
|
| 93 |
+
image_input,
|
| 94 |
+
color_mode_input,
|
| 95 |
+
hierarchical_input,
|
| 96 |
+
mode_input,
|
| 97 |
+
filter_speckle_input,
|
| 98 |
+
color_precision_input,
|
| 99 |
+
layer_difference_input,
|
| 100 |
+
corner_threshold_input,
|
| 101 |
+
length_threshold_input,
|
| 102 |
+
max_iterations_input,
|
| 103 |
+
splice_threshold_input,
|
| 104 |
+
path_precision_input
|
| 105 |
+
]
|
| 106 |
+
outputs_list = [
|
| 107 |
+
svg_output_display,
|
| 108 |
+
svg_download_link
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
+
# Using gr.Interface() inside gr.Blocks is one way,
|
| 112 |
+
# but for more control with Blocks, you typically define event listeners.
|
| 113 |
+
# However, your original structure with gr.Interface is fine.
|
| 114 |
+
# To make it more "Blocks-idiomatic" with the defined components:
|
| 115 |
+
submit_button.click(
|
| 116 |
+
fn=convert_image,
|
| 117 |
+
inputs=inputs_list,
|
| 118 |
+
outputs=outputs_list
|
| 119 |
+
)
|
| 120 |
|
| 121 |
+
# If you prefer to keep your original gr.Interface structure:
|
| 122 |
+
# (Comment out the submit_button.click above and uncomment the gr.Interface below)
|
| 123 |
+
# gr.Interface(
|
| 124 |
+
# fn=convert_image,
|
| 125 |
+
# inputs=inputs_list, # or define them inline as you had
|
| 126 |
+
# outputs=outputs_list, # or define them inline
|
| 127 |
+
# # title and description can be handled by gr.Markdown elements at the top if using Blocks for layout
|
| 128 |
+
# )
|
| 129 |
+
|
| 130 |
+
# Launch the interface
|
| 131 |
+
# For Hugging Face Spaces, share=True is not needed and can sometimes cause issues.
|
| 132 |
+
# Spaces handles the public URL.
|
| 133 |
+
iface.launch()
|