在没有开罗的情况下,在 Python 中将 SVG-XML 从网站转换为 png

时间:2021-01-28 19:46:09

标签: python svg imagemagick png

我想将从网站检索到的 SVG-XML 有效负载转换为 png 文件,而不使用 cairo,最好使用 ImageMagick/wand。举个例子,这就是我所拥有的:

import base64
from bs4 import BeautifulSoup as bs
import requests
import wand.image

r = requests.get("http://www.codazen.com")
soup = bs(r.content, 'html.parser')
img_tags = soup.find_all('img')

urls = [img['src'] for img in img_tags]

svg_xml_url = urls[1] # points to logo image

encoded = svg_xml_url.replace("data:image/svg+xml;base64,","")
  = base64.b64decode(encoded)

with wand.image.Image(blob=decoded, format="svg") as image:
    png_image = image.make_blob("png32")
    
with open("logo.png", "wb") as f:
    f.write(png_image)

然而,生成的 png 图像是空的:只有白色。我究竟做错了什么?谢谢。

0 个答案:

没有答案