heuer / qrcode-artistic

Segno plugin to convert (Micro) QR Codes to Pillow/PIL
BSD 3-Clause "New" or "Revised" License
35 stars 5 forks source link

Is there a way to get io.BytesIO() from to_artistic() ? #8

Closed Endogen closed 3 years ago

Endogen commented 3 years ago

With the main module i can do something like this:

buff = io.BytesIO()
segno.make_qr("Some input").save(buff, kind="png", border=1, scale=10)
buff.getvalue()

Is there a way to do that also with artistic qr-codes?

heuer commented 3 years ago

Thanks for the suggestion, but this isn't possible yet.

Should be easy to add, though…

I'll keep this open as an reminder to support BytesIO

heuer commented 3 years ago

Sorry, I was wrong. It is already possible:

import io
import segno

qr = segno.make('Test', error='h')
out = io.BytesIO()
qr.to_artistic(background='background.jpg', target=out, scale=10, kind='jpg')
print(out.getvalue())

Just provide the "kind" parameter.

Endogen commented 3 years ago

I can't provide a URL with an image for background right? what would i do if i don't want to save that image on disk first? Let's say this image here: https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg

Or i would also have the BytesIO() object of the image that i could use. But passing that to background= doesn't seem to work

heuer commented 3 years ago

Untested but this may work:

import io
from urllib.request import urlopen
import segno

qr = segno.make('Test', error='h')
out = io.BytesIO()
url = 'https://interactive-examples.mdn.mozilla.net/media/cc0-images/grapefruit-slice-332-332.jpg'
bg_img = urlopen(url)
qr.to_artistic(background=bg_img, target=out, scale=10, kind='jpg')
print(out.getvalue())

Let me know if it doesn't work.

Endogen commented 3 years ago

Unfortunately doesn't work: AttributeError("<class 'segno.QRCode'> object has no attribute to_artistic")

heuer commented 3 years ago

The error seems to indicate that Segno cannot find the plugin.

Did you install segno and qrcode-artistic? (pip install qrcode-artistic)

Does the following work or do you get an ImportError?

import qrcode_artistic
Endogen commented 3 years ago

Jesus Christ... sorry! I deinstalled it until i find out how it works to pass in the image from the URL and totally forgot to install it again for the test...

It's working now. Thanks so much for this. Helped me a lot! 👍

heuer commented 3 years ago

Thanks for your questions. I added more information regarding streams to https://segno.readthedocs.io/en/latest/artistic-qrcodes.html

Endogen commented 3 years ago

Maybe you want to add this also:

b_in = io.BytesIO()
b_out = io.BytesIO()
qr = segno.make_qr("Some string")
qr.to_artistic(background=b_in, target=b_out, border=1, scale=10, kind='png')

I use that for example with a Telegram bot. I get an image asb_in, feed it to to_artistic() and then send a message with content of b_out. That way i don't need to get the image myself via URL and i also don't need to temporarily save it. Love it!

heuer commented 3 years ago

Thanks for the suggestion, I added an example acc. to your suggestion. See last paragraph of https://segno.readthedocs.io/en/latest/artistic-qrcodes.html