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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -8
app.py CHANGED
@@ -3,8 +3,13 @@ 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
 
@@ -14,27 +19,39 @@ def convert_svg_to_png(svg_file, size_input):
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
 
 
3
  from PIL import Image
4
  import os
5
 
6
+ import cairosvg
7
+ import xml.etree.ElementTree as ET
8
+ from PIL import Image
9
+
10
  def convert_svg_to_png(svg_file, size_input):
11
+ """Convert SVG to PNG with user-specified dimensions, preserving the entire content."""
12
+ # Input validation
13
  if svg_file is None or not size_input or not size_input.strip():
14
  return None, None, "Please upload an SVG file and specify a size."
15
 
 
19
 
20
  # Parse the size input
21
  try:
22
+ output_width, output_height = map(int, size_input.split('x'))
23
+ if output_width <= 0 or output_height <= 0:
24
  raise ValueError("Width and height must be positive")
25
  except ValueError:
26
  return None, None, "Invalid size format. Use 'width x height' (e.g., '800x600') with positive numbers."
27
 
28
+ # Parse the SVG to handle viewBox and set width/height if missing
29
+ try:
30
+ root = ET.fromstring(svg_content)
31
+ view_box = root.get('viewBox')
32
+ if view_box and 'width' not in root.attrib and 'height' not in root.attrib:
33
+ _, _, vb_width, vb_height = map(float, view_box.split())
34
+ root.set('width', str(vb_width))
35
+ root.set('height', str(vb_height))
36
+ svg_content = ET.tostring(root, encoding='utf-8')
37
+ except ET.ParseError:
38
+ return None, None, "Invalid SVG file."
39
+
40
  # Define output path
41
  output_path = "./output.png"
42
 
43
+ # Convert SVG to PNG with user-specified dimensions
44
  cairosvg.svg2png(
45
  bytestring=svg_content,
46
  write_to=output_path,
47
+ output_width=output_width,
48
+ output_height=output_height
49
  )
50
 
51
  # Load the PNG
52
  final_image = Image.open(output_path)
53
+ return final_image, output_path, f"PNG generated at {output_width}x{output_height} pixels."
54
+
55
  with gr.Blocks() as bl:
56
  gr.Markdown("# SVG to PNG Converter")
57