import xml.etree.ElementTree as ET from PIL import Image import cairosvg def show(svg_file): if svg_file is None: return None, None # Read the SVG content with open(svg_file.name, 'rb') as f: svg_content = f.read() # Parse SVG to extract width and height try: root = ET.fromstring(svg_content) width = root.get('width') height = root.get('height') if width and height: # Remove 'px' units and convert to float width = float(width.replace('px', '')) height = float(height.replace('px', '')) else: width = height = None except ET.ParseError: # Handle invalid SVG width = height = None # Define output path output_path = "./test.png" # Convert SVG to PNG with explicit size if available if width and height: cairosvg.svg2png( bytestring=svg_content, write_to=output_path, output_width=width, output_height=height ) else: # Fallback to default rendering if no size is specified cairosvg.svg2png(bytestring=svg_content, write_to=output_path) # Load the PNG as a PIL image png_image = Image.open(output_path) return png_image, output_path