Update app.py
Browse files
app.py
CHANGED
@@ -2,59 +2,61 @@ import cairosvg
|
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
import os
|
5 |
-
import xml.etree.ElementTree as ET
|
6 |
|
7 |
-
def
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
10 |
with open(svg_file.name, 'rb') as f:
|
11 |
svg_content = f.read()
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
elif crop_box and 'left' in crop_box:
|
34 |
-
left = crop_box['left']
|
35 |
-
top = crop_box['top']
|
36 |
-
width = crop_box['right'] - left
|
37 |
-
height = crop_box['bottom'] - top
|
38 |
-
else:
|
39 |
-
width, height = 2000, 2000
|
40 |
-
cairosvg.svg2png(bytestring=svg_content, write_to=output_path, output_width=width, output_height=height)
|
41 |
final_image = Image.open(output_path)
|
42 |
-
return final_image, output_path
|
43 |
|
44 |
with gr.Blocks() as bl:
|
45 |
-
gr.Markdown("# SVG to PNG Converter
|
|
|
46 |
with gr.Row():
|
47 |
with gr.Column():
|
48 |
svg_input = gr.File(label="Upload SVG File", file_types=[".svg"])
|
49 |
-
|
50 |
-
|
|
|
|
|
|
|
|
|
51 |
with gr.Column():
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
|
|
|
|
|
|
59 |
|
60 |
bl.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
import os
|
|
|
5 |
|
6 |
+
def convert_svg_to_png(svg_file, size_input):
|
7 |
+
"""Convert SVG to PNG with user-specified dimensions."""
|
8 |
+
if svg_file is None or not size_input or not size_input.strip():
|
9 |
+
return None, None, "Please upload an SVG file and specify a size."
|
10 |
+
|
11 |
+
# Read the SVG content
|
12 |
with open(svg_file.name, 'rb') as f:
|
13 |
svg_content = f.read()
|
14 |
+
|
15 |
+
# Parse the size input
|
16 |
+
try:
|
17 |
+
width, height = map(int, size_input.split('x'))
|
18 |
+
if width <= 0 or height <= 0:
|
19 |
+
raise ValueError("Width and height must be positive")
|
20 |
+
except ValueError:
|
21 |
+
return None, None, "Invalid size format. Use 'width x height' (e.g., '800x600') with positive numbers."
|
22 |
+
|
23 |
+
# Define output path
|
24 |
+
output_path = "./output.png"
|
25 |
+
|
26 |
+
# Convert SVG to PNG with exact specified dimensions
|
27 |
+
cairosvg.svg2png(
|
28 |
+
bytestring=svg_content,
|
29 |
+
write_to=output_path,
|
30 |
+
output_width=width,
|
31 |
+
output_height=height
|
32 |
+
)
|
33 |
+
|
34 |
+
# Load the PNG
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
final_image = Image.open(output_path)
|
36 |
+
return final_image, output_path, f"PNG generated at {width}x{height} pixels."
|
37 |
|
38 |
with gr.Blocks() as bl:
|
39 |
+
gr.Markdown("# SVG to PNG Converter")
|
40 |
+
|
41 |
with gr.Row():
|
42 |
with gr.Column():
|
43 |
svg_input = gr.File(label="Upload SVG File", file_types=[".svg"])
|
44 |
+
size_input = gr.Textbox(
|
45 |
+
label="Output Size (width x height)",
|
46 |
+
placeholder="e.g., 800x600"
|
47 |
+
)
|
48 |
+
convert_btn = gr.Button("Convert")
|
49 |
+
|
50 |
with gr.Column():
|
51 |
+
final_output = gr.Image(type='pil', label="Output PNG", height=800)
|
52 |
+
download_btn = gr.File(label="Download PNG")
|
53 |
+
status_output = gr.Textbox(label="Status", interactive=False)
|
54 |
+
|
55 |
+
# Connect the conversion function
|
56 |
+
convert_btn.click(
|
57 |
+
fn=convert_svg_to_png,
|
58 |
+
inputs=[svg_input, size_input],
|
59 |
+
outputs=[final_output, download_btn, status_output]
|
60 |
+
)
|
61 |
|
62 |
bl.launch()
|