Can anyone provide some insight as to how to get the code examples/snippets that ship with the “qrcode” module (in “/usr/share/doc/packages/python3-qrcode/README.rst”) for Python3 to work? In at least one case, the code example contains a basic run-time error. Are these just garbage and one should look elsewhere for examples of how to use this module?
Many of the usage examples seem to return – rather slowly, too – a result of the type PilImage type which I take it is a format you need to use the PilImage module to handle. Except the project site for that module doesn’t seem to have a module for Python3 yet. And none of the others seem to have been updated since 2009.
The “qr” executable that comes with the module will work from the command line and I suppose one could invoke that from within a Python3 script but this seems like a backasswards way to obtain the QR file. And one that leaves no way means customizing the result (i.e., size, etc.).
Any hints or pointers to sites that have found a way to use this module?
Looks like the source has been updated as recently as only a few months ago, so don’t know what source and documentation you’re looking at which so much older…
Try the usage examples on these pages,… If you still have problems, post again.
This is the tiny script that was provided in the REAMDE.rst (which is, BTW, exactly the same that is shown in the two links in one of the other replies — minus the pretty HTMLization). I added the shebang line and a print to see what was created.
#!/usr/bin/python3 # Added to the original example
import qrcode
from qrcode.image.pure import PymagingImage
img = qrcode.make( 'http://www.intel.com',
image_factory=PymagingImage) # Simple URL for testing
print( "%s
" % img ) # Added to the example
When run it returns:
<qrcode.image.pure.PymagingImage object at 0x7ffb0f100828>
Marvelous. It’s some new object type. Do I need to import something else to deal with it—i.e. write it to disk?
My initial attempt to save the QR image using a generic write-binary-data-to-file:
with open( 'qr_url.png', 'wb' ) as f:
f.write( img )
Failed. Turns out you have to save it to disk using:
with open( 'qr_url.png', 'wb' ) as f:
img.save( f )
Still scratching my head over that. It’ll likely make more sense later tonight.
And, of course the content above these examples describe how you can “prettify” your image to look differently in different ways while still retaining the readability.
Maybe you shouldn’t need to build your f variable and then save it?
Looks to me that the variable img is already ready to simply be saved immediately, ie
#!/usr/bin/python3 # Added to the original example
import qrcode
from qrcode.image.pure import PymagingImage
img = qrcode.make( 'http://www.intel.com',
image_factory=PymagingImage) # Simple URL for testing
img.save("Intel-qr.png")