codelion commited on
Commit
18a4938
·
verified ·
1 Parent(s): 07efcb2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -8
app.py CHANGED
@@ -1,8 +1,11 @@
1
- import xml.etree.ElementTree as ET
2
- from PIL import Image
3
  import cairosvg
 
 
 
 
4
 
5
  def show(svg_file):
 
6
  if svg_file is None:
7
  return None, None
8
 
@@ -16,9 +19,9 @@ def show(svg_file):
16
  width = root.get('width')
17
  height = root.get('height')
18
  if width and height:
19
- # Remove 'px' units and convert to float
20
- width = float(width.replace('px', ''))
21
- height = float(height.replace('px', ''))
22
  else:
23
  width = height = None
24
  except ET.ParseError:
@@ -37,8 +40,13 @@ def show(svg_file):
37
  output_height=height
38
  )
39
  else:
40
- # Fallback to default rendering if no size is specified
41
- cairosvg.svg2png(bytestring=svg_content, write_to=output_path)
 
 
 
 
 
42
 
43
  # Load the PNG as a PIL image
44
  png_image = Image.open(output_path)
@@ -53,7 +61,7 @@ with gr.Blocks() as bl:
53
  convert_btn = gr.Button("Convert")
54
 
55
  with gr.Column():
56
- png_output = gr.Image(type='pil', label="Converted PNG")
57
  download_btn = gr.File(label="Download PNG")
58
 
59
  # Connect the conversion function
 
 
 
1
  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 show(svg_file):
8
+ # Check if no file is uploaded
9
  if svg_file is None:
10
  return None, None
11
 
 
19
  width = root.get('width')
20
  height = root.get('height')
21
  if width and height:
22
+ # Remove units (like 'px') and convert to float
23
+ width = float(''.join(filter(str.isdigit, width)) or width)
24
+ height = float(''.join(filter(str.isdigit, height)) or height)
25
  else:
26
  width = height = None
27
  except ET.ParseError:
 
40
  output_height=height
41
  )
42
  else:
43
+ # Fallback to default rendering with a reasonable size
44
+ cairosvg.svg2png(
45
+ bytestring=svg_content,
46
+ write_to=output_path,
47
+ output_width=800, # Default width if not specified
48
+ output_height=600 # Default height if not specified
49
+ )
50
 
51
  # Load the PNG as a PIL image
52
  png_image = Image.open(output_path)
 
61
  convert_btn = gr.Button("Convert")
62
 
63
  with gr.Column():
64
+ png_output = gr.Image(type='pil', label="Converted PNG", height=800)
65
  download_btn = gr.File(label="Download PNG")
66
 
67
  # Connect the conversion function