File size: 1,239 Bytes
a6a908a a8aa56e 1b43408 a6a908a ee6a1c6 1b43408 a8aa56e 1b43408 a8aa56e a6a908a a8aa56e ee6a1c6 a8aa56e a6a908a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import cairosvg
import gradio as gr
from PIL import Image
import os
from io import BytesIO
def show(svg_file):
# Check if svg_file is None (no file uploaded)
if svg_file is None:
return None, None
# Get the file content from the Gradio file object
# svg_file is a tempfile._TemporaryFileWrapper or similar object
with open(svg_file.name, 'rb') as f:
svg_content = f.read()
# Convert SVG content to PNG
output_path = "./test.png"
cairosvg.svg2png(bytestring=svg_content, write_to=output_path)
# Open and return the PNG image
png_image = Image.open(output_path)
return png_image, output_path
with gr.Blocks() as bl:
gr.Markdown("# SVG to PNG Converter")
with gr.Row():
with gr.Column():
svg_input = gr.File(label="Upload SVG File", file_types=[".svg"])
convert_btn = gr.Button("Convert")
with gr.Column():
png_output = gr.Image(type='pil', label="Converted PNG")
download_btn = gr.File(label="Download PNG")
# Connect the conversion function
convert_btn.click(
fn=show,
inputs=svg_input,
outputs=[png_output, download_btn]
)
bl.launch() |