mozman / svgwrite

Python Package to write SVG files (UNMAINTAINED)
Other
547 stars 97 forks source link

The image write isn't working for me #97

Open Tylersuard opened 3 years ago

Tylersuard commented 3 years ago

I write an image, and it just shows up as a blank svg.

I'm using this code:

import svgwrite import base64 from wand.image import Image

dwg = svgwrite.Drawing('svgwrite-example1.svg', profile='tiny') img = Image(filename="small_electric.png")

image_data = img.make_blob(format='png') encoded = base64.b64encode(image_data).decode() pngdata = 'data:image/png;base64,{}'.format(encoded)

image = dwg.add(dwg.image(href=(pngdata)))

dwg.save()

aqujesus commented 3 years ago

probably the error is in the encode part, have you checked the XML file, should be something like: "<image xlink:href="data:image/png;base64,B4s3G4str1ng...

I have used:

with open("small_electric.png", "rb") as image_file:
    encoded_image = base64.b64encode(image_file.read())

encoded = encoded_image.decode()