Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
661 views
in Technique[技术] by (71.8m points)

Convert SVG-XML from website into png in Python without cairo

I would like to take an SVG-XML payload retrieved from a website to a png file without using cairo, ideally using ImageMagick/wand. As an example, here's what I've got:

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)

However, the resulting png image is eempty: just white. What am I doing wrong? Thanks.

question from:https://stackoverflow.com/questions/65944187/convert-svg-xml-from-website-into-png-in-python-without-cairo

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...