1

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.

acciolurker
  • 429
  • 4
  • 15
  • Does this answer your question? [Convert SVG to PNG with Python on Windows](https://stackoverflow.com/questions/65759849/convert-svg-to-png-with-python-on-windows) – Peter O. Jan 29 '21 at 12:57
  • Turns out that the code works fine, I just had a white image on a white background. – acciolurker Jan 29 '21 at 16:34
  • See the 3rd answer here https://stackoverflow.com/questions/51450134/how-to-convert-svg-to-png-or-jpeg-in-python for a wand solution. – Mark Kortink Jul 03 '21 at 22:17

0 Answers0