Skip to main content

PyQRCode New Generation -- A QR code generator written purely in Python with SVG, EPS, PNG and terminal output.

Project description

PyQRCodeNG

The PyQRCodeNG module is a QR code generator that is simple to use and written in pure Python. The module automates most of the building process for creating QR codes. Most codes can be created using only two lines of code!

Unlike other generators, all of the helpers can be controlled manually. You are free to set any or all of the properties of your QR code.

QR codes can be saved as SVG, XBM, EPS, PNG (by using the PyPNG <https://pypi.org/project/pypng/> module), or plain text. They can also be displayed directly in most Linux terminal emulators and Tkinter. PIL or Pillow are not used to render the image files.

The PyQRCodeNG module attempts to follow the QR code standard as closely as possible. The terminology and the encodings used in PyQRCodeNG come directly from the standard. This module also follows the algorithm laid out in the standard.

Requirements

PyQRCodeNG only requires Python 2.7 or Python 3. You may want to install PyPNG in order to render PNG files, but it is optional.

Installation

Installation is simple. It can be installed from pip using the following command:

$ pip install -U pyqrcodeng

Replacing PyQRCode with PyQRCodeNG

PyQRCodeNG is a fork of PyQRCode since the latter seems to be unmaintained. The API is mainly compatible to PyQRCode. In your code you can use the following import without changing the QR Code generation code.

>>> import pyqrcodeng as pyqrcode

Usage

This is the only import you need. The heart of the module is the QRCode class. You can construct the class normally, or use the create wrapper function.

>>> import pyqrcodeng
>>> qr = pyqrcodeng.create('Unladden swallow')
>>> qr.png('famous-joke.png', scale=5)

Encoding Data

This module supports all four encodings for data: numeric, alphanumeric, kanji, and binary.

The numeric type is the most efficient way to encode digits. As the name implies it is designed to encode integers. Some numbers might be too large, the object can use a string containing only digits instead of an actual number.

>>> number = pyqrcodeng.create(123456789012345)

The alphanumeric type is very limited in that it can only encode some ASCII characters. It encodes: uppercase letters, 0-9, the horizontal space, and eight punctuation characters. The available characters will let you encode a URL

>>> url = pyqrcodeng.create('http://uca.edu')

When all else fails the data can be encoded in pure binary. The quotation below must be encoded in binary because of the lower-cased characters, the apostrophe and the new line character.

>>> life = pyqrcodeng.create('''MR. CREOSOTE: Better get a bucket. I'm going to throw up.
    MAITRE D: Uh, Gaston! A bucket for monsieur. There you are, monsieur.''')

The only unimplemented encoding is ECI mode which allows for multiple encodings in one QR code (this will be implemented in a future version).

Manually Setting The QR Code’s Properties

There are many situation where you might wish to have more fine grained control over how the QR Code is generated. You can specify all the properties of your QR code through the create function. There are three main properties to a QR code.

The error parameter sets the error correction level of the code. Each level has an associated name given by a letter: L, M, Q, or H; each level can correct up to 7, 15, 25, or 30 percent of the data respectively. There are several ways to specify the level, see pyqrcodeng.tables.modes for all the possible values. By default this parameter is set to ‘H’ which is the highest possible error correction, but it has the smallest available data capacity.

The version parameter specifies the size and data capacity of the code. Versions are any integer between 1 and 40, where version 1 is the smallest QR code, and version 40 is the largest. By default, the object uses the data’s encoding and error correction level to calculate the smallest possible version. You may want to specify this parameter for consistency when generating several QR codes with varying amounts of data. That way all of the generated codes would have the same size.

Finally, the mode parameter sets how the contents will be encoded. As mentioned above, three of the five possible encodings have been written. By default the object uses the most efficient encoding for the contents. You can change this though. See qrcode.tables.modes for a list of possible values for this parameter.

The code below constructs a QR code with 25% error correction, size 27, and forces the encoding to be binary (rather than numeric).

>>> big_code = pyqrcodeng.create('0987654321', error='L', version=27, mode='binary')

Rendering

There are many possible formats for rendering the QR Code. The first is to render it as a string of 1’s and 0’s. This is method is used to help end users create their own renderer. It is also possible to print the code such that it is directly displayable in most Linux terminals. There are several image based renderers.

The terminal renderer outputs a string of ASCII escape codes that when displayed in a compatible terminal, will display a valid QR code. The background and module colors are settable (although as with any time you display colors in the terminal, there are several caveats).

>>> url.term()

The SVG renderer outputs the QR Code as a scalable vector graphic. This renderer does not require any external modules. Instead it hand draws the QR code as a set paths.

>>> url.svg(sys.stdout, scale=1)
>>> url.svg('uca.svg', scale=4, module_color="#7D007D")

Alternatively, if you install the pypng module, you can render the QR Code to a PNG file. Colors should be specified as RGB or RGBA if you want to take advantage of transparency.

>>> number.png('big-number.png')
>>> life.png('sketch.png', scale=6, module_color=(0, 0, 0, 128), background=(0xff, 0xff, 0xcc))

Finally, there is a text based renderer. This will output the QR code as a string of 1’s and 0’s, with each row of the code on a new line.

>>> print(number.text())

Documentation

Read the online documentation at <https://pyqrcodeng.readthedocs.io/>

Changes

1.3.6 - 2020-01-01

  • Code improvements / refactorings

  • Optimized generation of numerical QR Codes

  • MUCH faster PNG writing (approx. twice as fast)

1.3.5 - 2020-01-01

  • Don’t raise a UnicodeError if the user provided binary data and sets the mode explicitly to ‘binary’. Fixed by Mathieu <https://github.com/albatros69> (PR #13)

  • The builder raises more specific exceptions like VersionError, MaskError, DataOverflowError. This change is backwards compatible since all exceptions are inherited from ValueError.

  • Added “designator” property to QRCode. This encodes the version number and error correction level, i.e. “1-H”

  • Fixed alphanumeric encoding. The bug was introduced in the 1.3.4 release

  • Added more test cases

1.3.4 - 2019-07-07

  • Improved documentation

  • Internal code refactoring

  • Added CLI docs

  • (Deprecated) QRCode.terminal() did not work. Fixed.

1.3.3 - 2019-06-30

  • Improved documentation

  • Internal code refactoring

  • Performance improvements

1.3.2 - 2019-06-29

  • Initial release of PyQRCode NG (PyQRCode Next Generation)

1.3.0 - 2018-06-26

1.2.1 - 2016-06-20

  • Fixed issue #43. A debug print statement got left in by mistake. I altered The distribution script to check and make sure it does not happen again.

1.2 - 2016-05-20

  • Added Kanji support.

  • Added ability to output PNG QR codes as a base64 string. Allows coded to be created for web services without the need to create intermediary files. Thanks to [Fábio C. Barrionuevo da Luz (luzfcb)](https://github.com/luzfcb)

  • Added renderer for XBM. Displaying QR codes in Tkinter is now extremely simple. Thanks to [Seth VanHeulen (svanheulen)](https://github.com/svanheulen)

1.1.1 - 2016-02-27

  • Fix for issue #38, where numeric encodings got broken by added unicode support.

1.1 - 2016-04-15

  • Added support for Python 2.6

  • All renderers now have a quiet zone of four. This value is settable via a parameter.

  • Fixed issue where file streams were not being closed correctly

  • Special thanks goes to [Lars (heuer)](https://github.com/heuer) who contributed a massive amount of improvements in this version.

    • Enormously improved SVG implementation. Now uses paths instead of lines. Also allows for SVG fragments instead of entire documents.

    • We now have unit tests!! He wrote over 100 unit tests for all of the various parts of the library.

    • A new EPS renderer.

    • A mechanism for showing QR codes directly from within your code.

1.0 - 2014-12-04

  • Fixed issue where terminal bits were being added in the wrong location.

  • Added ability to output QR code to a Linux terminal.

  • Added support for Python 2.7

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

PyQRCodeNG-1.3.6.tar.gz (46.4 kB view details)

Uploaded Source

Built Distribution

PyQRCodeNG-1.3.6-py2.py3-none-any.whl (47.5 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file PyQRCodeNG-1.3.6.tar.gz.

File metadata

  • Download URL: PyQRCodeNG-1.3.6.tar.gz
  • Upload date:
  • Size: 46.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for PyQRCodeNG-1.3.6.tar.gz
Algorithm Hash digest
SHA256 b84cd27cbf973593686d38b5a002464fbeb6e8dbd16aea293e6092eea0dc462b
MD5 4e40e90b9cfd59b7403d1d0ea4a8b715
BLAKE2b-256 b041f40da7cff47b8055103262b3783fec3ac97059f44a2656894328073dcb72

See more details on using hashes here.

File details

Details for the file PyQRCodeNG-1.3.6-py2.py3-none-any.whl.

File metadata

  • Download URL: PyQRCodeNG-1.3.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 47.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.4.2 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.3

File hashes

Hashes for PyQRCodeNG-1.3.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 40ac27bae84139cdbbe72ef80ee20c1506452af4458ef5305cf02ad19ac7787e
MD5 3366c4ae87d2ef93ca2810a03ff42845
BLAKE2b-256 7ae7d5c8834c3f1b122c8a517bf7264182a16fa1e2b8d910da3d9bf927f3816e

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page