QRcode module for Python3

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?

How did you install it? Can you post the errors?

With a quick Internet search, I see pages and pages of articles written about this app…

Let’s just start with the info posted at PyPi (The official Python repos) and the Project’s source (they’re nearly identical)
https://pypi.org/project/qrcode/
https://github.com/lincolnloop/python-qrcode

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.

TSU

How? Using Yast.

This is a sample from the README.rst file (I’m looking to create a PNG file):

Pure Python PNG

Install the following two packages::

pip install git+git://github.com/ojii/pymaging.git#egg=pymaging
pip install git+git://github.com/ojii/pymaging-png.git#egg=pymaging-png

From your command line::

qr --factory=pymaging "Some text" > test.png

Or in Python::

The “qr” command example works fine.

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.

Don’t scratch your head, don’t burn up any more grey cells.

Simply use the examples at the end of one of my links (reposted below) to encode and create an SVG or PNG file
https://github.com/lincolnloop/python-qrcode

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.

TSU

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")

Speculating,
TSU