Results 1 to 6 of 6

Thread: QRcode module for Python3

  1. #1
    Join Date
    Feb 2013
    Location
    N.E. Illinois
    Posts
    17

    Default 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?

  2. #2
    Join Date
    Apr 2016
    Location
    North America
    Posts
    537

    Default Re: QRcode module for Python3

    How did you install it? Can you post the errors?
    Tumbleweed, Xfce, Firefox, uBlock Origin

  3. #3
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,278
    Blog Entries
    2

    Default Re: QRcode module for Python3

    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
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  4. #4
    Join Date
    Feb 2013
    Location
    N.E. Illinois
    Posts
    17

    Default Re: QRcode module for Python3

    Quote Originally Posted by ravas View Post
    How did you install it? Can you post the errors?
    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.
    Code:
    #!/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\n" % img )        # Added to the example
    When run it returns:
    Code:
    <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:
    Code:
    with open( 'qr_url.png', 'wb' ) as f:
         f.write( img )
    Failed. Turns out you have to save it to disk using:
    Code:
    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.

  5. #5
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,278
    Blog Entries
    2

    Default Re: QRcode module for Python3

    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
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  6. #6
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,278
    Blog Entries
    2

    Default Re: QRcode module for Python3

    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
    Code:
    #!/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
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •