Svg-Tracer-New / app.py
gdTharusha's picture
Create app.py
eb24b38 verified
raw
history blame
No virus
1.38 kB
import gradio as gr
from svgtrace import trace_image
import io
def convert_image_to_svg(image, threshold=128, line_thickness=1.0):
# Convert the uploaded image to bytes
image_bytes = io.BytesIO()
image.save(image_bytes, format='PNG')
image_bytes = image_bytes.getvalue()
# Convert the image to SVG using custom tracing options
svg = trace_image(image_bytes, threshold=threshold, line_thickness=line_thickness)
# Save the SVG output to a file
output_svg = 'output_image.svg'
with open(output_svg, 'w') as f:
f.write(svg)
return output_svg
# Set up Gradio interface with customizable options
def create_gradio_interface():
inputs = [
gr.inputs.Image(type="pil", label="Upload Image"),
gr.inputs.Slider(0, 255, default=128, label="Threshold"),
gr.inputs.Slider(0.1, 5.0, default=1.0, label="Line Thickness")
]
outputs = gr.outputs.File(label="Download SVG")
interface = gr.Interface(
fn=convert_image_to_svg,
inputs=inputs,
outputs=outputs,
title="Image to SVG Converter",
description="Upload an image and customize the options to convert it to a high-quality SVG file."
)
return interface
# Launch the Gradio interface
if __name__ == "__main__":
interface = create_gradio_interface()
interface.launch()