codelion commited on
Commit
7f643c5
·
verified ·
1 Parent(s): e183623

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -44
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 initial_render(svg_file):
8
- if svg_file is None:
9
- return None, None
 
 
 
10
  with open(svg_file.name, 'rb') as f:
11
  svg_content = f.read()
12
- temp_svg_path = "./temp.svg"
13
- with open(temp_svg_path, 'wb') as f:
14
- f.write(svg_content)
15
- output_path = "./initial_preview.png"
16
- cairosvg.svg2png(bytestring=svg_content, write_to=output_path, output_width=3000, output_height=3000)
17
- preview_image = Image.open(output_path)
18
- return preview_image, temp_svg_path
19
-
20
- def final_convert(svg_path, crop_box, size_input):
21
- if svg_path is None:
22
- return None, None
23
- with open(svg_path, 'rb') as f:
24
- svg_content = f.read()
25
- output_path = "./final_output.png"
26
- if size_input and size_input.strip():
27
- try:
28
- width, height = map(int, size_input.split('x'))
29
- if width <= 0 or height <= 0:
30
- raise ValueError("Width and height must be positive")
31
- except ValueError:
32
- return gr.Warning("Invalid size format. Use 'width x height' (e.g., '800x600')"), None
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 with Custom Crop or Size")
 
46
  with gr.Row():
47
  with gr.Column():
48
  svg_input = gr.File(label="Upload SVG File", file_types=[".svg"])
49
- preview_btn = gr.Button("Generate Preview")
50
- size_input = gr.Textbox(label="Output Size (width x height)", placeholder="e.g., 800x600 (optional, overrides crop)")
 
 
 
 
51
  with gr.Column():
52
- preview_output = gr.Image(type='pil', label="Preview (Draw a box to crop if no size specified)", interactive=True, height=800)
53
- crop_btn = gr.Button("Convert")
54
- final_output = gr.Image(type='pil', label="Final PNG", height=800)
55
- download_btn = gr.File(label="Download Final PNG")
56
- svg_state = gr.State()
57
- preview_btn.click(fn=initial_render, inputs=svg_input, outputs=[preview_output, svg_state])
58
- crop_btn.click(fn=final_convert, inputs=[svg_state, preview_output, size_input], outputs=[final_output, download_btn])
 
 
 
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()