Displaying QR Code PIL Image object

498 views
Skip to first unread message

Ben Sharif

unread,
Oct 1, 2014, 8:23:14 PM10/1/14
to web...@googlegroups.com
I am generating a qr code using python module qrcode.

I am not needing to store the qr code image in a database, just generate it each time.

controller:
def create_qr():
    text = "hello world qr code"
    qr_code = qrcode.make(text)   

view:
<img src="{{=qr_code}}">


doing the above, the controller is returning a PIL Image object but trying to embed that image is producing no actual image. how do I get web2py to display a PIL image object in the view?


Massimo Di Pierro

unread,
Oct 3, 2014, 7:41:54 AM10/3/14
to web...@googlegroups.com
<img src="...">

is supposed to be a link to an image, not an image. If you want to inline the binary data you can try:

import base64
encoded_qr_code = base64.b64encode(qr_code)

and

<img src="data:image/gif;base64,{{=encoded_qr_code}}">

May not work on older browsers.
Reply all
Reply to author
Forward
0 new messages