|
import cairosvg |
|
import gradio as gr |
|
from PIL import Image |
|
import os |
|
|
|
def convert_svg_to_png(svg_file, size_input): |
|
"""Convert SVG to PNG with user-specified dimensions.""" |
|
if svg_file is None or not size_input or not size_input.strip(): |
|
return None, None, "Please upload an SVG file and specify a size." |
|
|
|
|
|
with open(svg_file.name, 'rb') as f: |
|
svg_content = f.read() |
|
|
|
|
|
try: |
|
width, height = map(int, size_input.split('x')) |
|
if width <= 0 or height <= 0: |
|
raise ValueError("Width and height must be positive") |
|
except ValueError: |
|
return None, None, "Invalid size format. Use 'width x height' (e.g., '800x600') with positive numbers." |
|
|
|
|
|
output_path = "./output.png" |
|
|
|
|
|
cairosvg.svg2png( |
|
bytestring=svg_content, |
|
write_to=output_path, |
|
output_width=width, |
|
output_height=height |
|
) |
|
|
|
|
|
final_image = Image.open(output_path) |
|
return final_image, output_path, f"PNG generated at {width}x{height} pixels." |
|
|
|
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"]) |
|
size_input = gr.Textbox( |
|
label="Output Size (width x height)", |
|
placeholder="e.g., 800x600" |
|
) |
|
convert_btn = gr.Button("Convert") |
|
|
|
with gr.Column(): |
|
final_output = gr.Image(type='pil', label="Output PNG", height=800) |
|
download_btn = gr.File(label="Download PNG") |
|
status_output = gr.Textbox(label="Status", interactive=False) |
|
|
|
|
|
convert_btn.click( |
|
fn=convert_svg_to_png, |
|
inputs=[svg_input, size_input], |
|
outputs=[final_output, download_btn, status_output] |
|
) |
|
|
|
bl.launch() |