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.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.4.tar.gz (49.6 kB view hashes)

Uploaded Source

Built Distribution

PyQRCodeNG-1.3.4-py2.py3-none-any.whl (46.7 kB view hashes)

Uploaded Python 2 Python 3

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