lincolnloop / python-qrcode

Python QR Code image generator
https://pypi.python.org/pypi/qrcode
Other
4.36k stars 666 forks source link

Add tostring() method to SVG images. #189

Closed Dreamsorcerer closed 3 years ago

Dreamsorcerer commented 4 years ago

SVGs are particularly useful to have in string format, which can be output directly into HTML etc.

It took me far to long to figure out how to do this, so hopefully this will save other people some time.

As a side note, the SVGPathImage in the _write() method (which I've based this code on), appends the path code to the image element. Wouldn't this mess up the image if the save() method is called twice? Edit: Seems to be a fix #137

kaos commented 4 years ago

There is a workable approach without modifiying the lib, too. (this should be in the readme, how to use the img presented in the example)

import io
f = io.BytesIO()
img.save(f)
f.getvalue()  ==> b'<?xml ....'
Dreamsorcerer commented 4 years ago

You can use the code in this PR without modifying the lib anyway.. ET.tostring(img._img)