Spaces:
Running
Running
Upload 2 files
Browse files- app.py +93 -0
- requirements.txt +3 -0
app.py
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import io
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import vtracer
|
| 5 |
+
import tempfile
|
| 6 |
+
|
| 7 |
+
def convert_image(image, color_mode, hierarchical, mode, filter_speckle,
|
| 8 |
+
color_precision, layer_difference, corner_threshold,
|
| 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(),
|
| 24 |
+
filter_speckle=int(filter_speckle),
|
| 25 |
+
color_precision=int(color_precision),
|
| 26 |
+
layer_difference=int(layer_difference),
|
| 27 |
+
corner_threshold=int(corner_threshold),
|
| 28 |
+
length_threshold=float(length_threshold),
|
| 29 |
+
max_iterations=int(max_iterations),
|
| 30 |
+
splice_threshold=int(splice_threshold),
|
| 31 |
+
path_precision=int(path_precision)
|
| 32 |
+
)
|
| 33 |
+
|
| 34 |
+
# Save the SVG string to a temporary file
|
| 35 |
+
temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.svg')
|
| 36 |
+
temp_file.write(svg_str.encode('utf-8'))
|
| 37 |
+
temp_file.close()
|
| 38 |
+
|
| 39 |
+
return gr.HTML(f'<svg viewBox="0 0 {image.width} {image.height}">{svg_str}</svg>'), temp_file.name
|
| 40 |
+
|
| 41 |
+
# HTML code for the ad banner
|
| 42 |
+
ad_code = """
|
| 43 |
+
<div style="text-align: center;">
|
| 44 |
+
<iframe src="https://www.fiverr.com/gig_widgets?id=U2FsdGVkX1+9vQaosK0mq7xGXzIb/5hdFYIQsmHlpeMwFm5gingveDZKXBz2oJwV7ZwvToxseOx/KzKtr77ryB3qTb83/+GvCyI8OKpZcSgM0sDHzEPlZnqNeu8Db4y/IXlAceA1UcYT5Z3cW5vBLrgJ/WOpyMWSiyfwwMmGngjltQBexcivW7ukRd+/0yos7GCbfGjGdnLPHm2LB/CB9w2mA1i8cVv9LU+UUdj/O5KLRXtnychl9wAMkPTpmwMiyuAfUZy0nbK/Xa5O2UecmCvn7wicTr0TCTpLmIxaEh3YftXOVr9e36OGVYTmy5nlsgujkqJPI7wL0dXfon0ru4kvoycC7UD6m/whMoxrWZOf386qV2eEeXzk3vKO/emXUUfSavtofzSBtjLcyAOVhnHJg27PvVPgvFTQdy0o3F1M0DaHYTVW3Ln45MPYvMSH&affiliate_id=36184&strip_google_tagmanager=true" loading="lazy" data-with-title="false" class="fiverr_nga_frame" frameborder="0" height="350" width="100%" referrerpolicy="no-referrer-when-downgrade" data-mode="random_gigs" onload=" var frame = this; var script = document.createElement('script'); script.addEventListener('load', function() { window.FW_SDK.register(frame); }); script.setAttribute('src', 'https://www.fiverr.com/gig_widgets/sdk'); document.body.appendChild(script); " ></iframe>
|
| 45 |
+
</div>
|
| 46 |
+
"""
|
| 47 |
+
|
| 48 |
+
# HTML code for the ad banner
|
| 49 |
+
ad_code2 = """
|
| 50 |
+
<div style="text-align: center;">
|
| 51 |
+
<a href="https://beta.publishers.adsterra.com/referral/UNXJYTziBP" target="_blank" style="display: inline-block;">
|
| 52 |
+
<img decoding="async" alt="banner" src="https://landings-cdn.adsterratech.com/referralBanners/gif/468x120_adsterra_reff.gif">
|
| 53 |
+
</a>
|
| 54 |
+
<a href="https://go.fiverr.com/visit/?bta=36184&brand=fiverrcpa&landingPage=https%253A%252F%252Fwww.fiverr.com%252Fcategories%252Fprogramming-tech%252Fai-coding%252Fai-applications%253Fsource%253Dcategory_tree" target="_blank" style="display: inline-block;">
|
| 55 |
+
<img fetchpriority="high" decoding="async" width="468" height="120" src="https://ziverr.xyz/wp-content/uploads/2024/06/PASSIVE-1.gif" class="attachment-large size-large wp-image-1266" alt="">
|
| 56 |
+
</a>
|
| 57 |
+
</div>
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
# Gradio interface
|
| 61 |
+
iface = gr.Blocks()
|
| 62 |
+
|
| 63 |
+
with iface:
|
| 64 |
+
|
| 65 |
+
gr.HTML(ad_code2)
|
| 66 |
+
|
| 67 |
+
gr.Interface(
|
| 68 |
+
fn=convert_image,
|
| 69 |
+
inputs=[
|
| 70 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 71 |
+
gr.Radio(choices=["Color", "Binary"], value="Color", label="Color Mode"),
|
| 72 |
+
gr.Radio(choices=["Stacked", "Cutout"], value="Stacked", label="Hierarchical"),
|
| 73 |
+
gr.Radio(choices=["Spline", "Polygon", "None"], value="Spline", label="Mode"),
|
| 74 |
+
gr.Slider(minimum=1, maximum=10, value=4, step=1, label="Filter Speckle"),
|
| 75 |
+
gr.Slider(minimum=1, maximum=8, value=6, step=1, label="Color Precision"),
|
| 76 |
+
gr.Slider(minimum=1, maximum=32, value=16, step=1, label="Layer Difference"),
|
| 77 |
+
gr.Slider(minimum=10, maximum=90, value=60, step=1, label="Corner Threshold"),
|
| 78 |
+
gr.Slider(minimum=3.5, maximum=10, value=4.0, step=0.5, label="Length Threshold"),
|
| 79 |
+
gr.Slider(minimum=1, maximum=20, value=10, step=1, label="Max Iterations"),
|
| 80 |
+
gr.Slider(minimum=10, maximum=90, value=45, step=1, label="Splice Threshold"),
|
| 81 |
+
gr.Slider(minimum=1, maximum=10, value=8, step=1, label="Path Precision")
|
| 82 |
+
],
|
| 83 |
+
outputs=[
|
| 84 |
+
gr.HTML(label="SVG Output"),
|
| 85 |
+
gr.File(label="Download SVG")
|
| 86 |
+
],
|
| 87 |
+
title="Convert Image to SVG vectors",
|
| 88 |
+
description="Upload an image and customize the conversion parameters as needed.",
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
gr.HTML(ad_code)
|
| 92 |
+
|
| 93 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
pillow
|
| 3 |
+
vtracer
|