What I’m trying to do:
create a QR code image
What I’ve tried and what’s not working:
I’ve tried the code below but cant figure out how to turn the resulting qrcode image into a media object
Code Sample:
@anvil.server.callable
def make_qrcode(size=10):
# Link for website
input_data = "https://towardsdatascience.com/face-detection-in-10-lines-for-beginners-1787aa1d9127"
#Creating an instance of qrcode
qr = qrcode.QRCode(
version=1,
box_size=size,
border=5)
qr.add_data(input_data)
qr.make(fit=True)
qrimg = qr.make_image(fill='black', back_color='white')
print(type(qrimg))
my_media = anvil.BlobMedia(content_type="image/png", content=qrimg, name="iron_man.png")
table_row = app_tables.butler_vars.get(name='qr_code')
table_row['media'] = my_media
I found this on the forum but I get errors about not being a string etc: Can I write to a Media object as a file? - #2
I know I must be missing something basic.
Many thanks. ![:slight_smile: :slight_smile:](https://anvil.works/forum/images/emoji/apple/slight_smile.png?v=10)
With out looking at the qrcode
library, I could be well off track but the BlobMedia is expecting a binary string rather than an image as probably returned by the qr.make_image()
- see the BlobMedia Docs. So I think you would need to turn your image into a byte string to be able to save it as a BlobMedia.
I use pyqrcode
(docs page) and return a binary representation of the image with the following approach (adapted to your code but untested):
qr_binary = pyqrcode.create(input_data, mode = 'binary')
# Convert the qr code image (png format)
filename = "iron_man"
file_path = f"/tmp/{filename}"
qr_binary.png(file, scale=6)
with open(file_path, 'rb') as f:
my_media= anvil.BlobMedia('image/png', f.read(), name=filename)
table_row = app_tables.butler_vars.get(name='qr_code')
table_row['media'] = my_media
1 Like
thanks so much @rickhurlbatt
will this cause a lot of files to be saved in the “/tmp/” folder?
I think the temp folder is cleared periodically by Anvil based on this in the docs. Regardless, the with
block should destroy the file created when it completes. So no, you shouldn’t have plenty of files lying around unless I have made an error.
That said, file handling is definitely not my strength and this is a very old piece of my code so I am sure it could be done in a much cleaner way.
Would be keen to hear some feedback on this code from others and we can both learn something ![:slight_smile: :slight_smile:](https://anvil.works/forum/images/emoji/apple/slight_smile.png?v=10)
I have no experience with this stuff, but here are my musings anyway.
If it’s the qr_binary.png()
line that creates the file, I don’t think the with
block will delete it.
Also, instead of the with
block, maybe you can just do:
my_media = anvil.media.from_file(file_path, 'image/png', name=filename)
You could also avoid creating files by using a BytesIO instance:
import io
import pyqrcode
import anvil
import anvil.server
from anvil.tables import app_tables
@anvil.server.callable
def make_qrcode(size=10):
input_data = "https://towardsdatascience.com/face-detection-in-10-lines-for-beginners-1787aa1d9127"
filename = "iron_man"
qr_binary = pyqrcode.create(input_data, mode="binary")
buffer = io.BytesIO()
qr_binary.png(buffer, scale=6)
buffer.seek(0)
my_media = anvil.BlobMedia("image/png", buffer.read(), name=filename)
table_row = app_tables.butler_vars.get(name="qr_code")
table_row["media"] = my_media
5 Likes