|
import xml.etree.ElementTree as ET |
|
from PIL import Image |
|
import cairosvg |
|
|
|
def show(svg_file): |
|
if svg_file is None: |
|
return None, None |
|
|
|
|
|
with open(svg_file.name, 'rb') as f: |
|
svg_content = f.read() |
|
|
|
|
|
try: |
|
root = ET.fromstring(svg_content) |
|
width = root.get('width') |
|
height = root.get('height') |
|
if width and height: |
|
|
|
width = float(width.replace('px', '')) |
|
height = float(height.replace('px', '')) |
|
else: |
|
width = height = None |
|
except ET.ParseError: |
|
|
|
width = height = None |
|
|
|
|
|
output_path = "./test.png" |
|
|
|
|
|
if width and height: |
|
cairosvg.svg2png( |
|
bytestring=svg_content, |
|
write_to=output_path, |
|
output_width=width, |
|
output_height=height |
|
) |
|
else: |
|
|
|
cairosvg.svg2png(bytestring=svg_content, write_to=output_path) |
|
|
|
|
|
png_image = Image.open(output_path) |
|
return png_image, output_path |