Creating QR Codes or Micro QR Codes

Segno’s (Micro) QR Codes are independent of a concrete output format; it’s possible to create more than one rendering (output format) from a single QR Code or Micro QR Code:

>>> import segno
>>> qr = segno.make('Henry Lee')
>>> qr.save('henry-lee.svg')  # SVG document
>>> qr.save('henry-lee.png')  # PNG image
>>> qr.save('henry-lee.eps')  # EPS document
>>> qr.save('henry-lee.txt')  # Text output

By default, the serialized (Micro) QR Codes are black and have a quiet zone (border) of four (or two for Micro QR Codes) modules. Nearly all output methods provide options to change the color and border.

>>> import segno
>>> qr = segno.make('You Know My Name (Look Up The Number)')
>>> qr.save('you-know-my-name-no-border.svg', border=0)  # no border / quiet zone
>>> qr.save('you-know-my-name-color-green.svg', color='green')  # default border, dark modules are green
>>> qr.save('you-know-my-name-background-grey.svg', background='#eee')  # default border, background grey

The factory function segno.make() chooses the minimal possible (Micro) QR Code for the provided input.

>>> import segno
>>> qr = segno.make('Rain')
>>> qr.version
'M3'

The caller may enforce that a QR Code instead of a Micro QR Code should be generated even if the content may fit into a Micro QR Code.

>>> import segno
>>> qr = segno.make('Rain', micro=False)
>>> qr.version
1

Further, Segno provides two additional factory functions to enforce the creation of QR Codes or Micro QR Codes: segno.make_qr() for QR Codes and segno.make_micro() to create Micro QR Codes:

>>> import segno
>>> mqr = segno.make_micro('The Beatles')  # Micro QR Code
>>> mqr.designator  # Get the version and error level
'M4-M'
>>> qr = segno.make_qr('The Beatles')  # Same content but as QR Code
>>> qr.designator
'1-Q'

If the provided content is too large, a segno.DataOverflowError is thrown:

>>> import segno
>>> qr = segno.make_micro('The Curse of Millhaven')
Traceback (most recent call last):
    ...
DataOverflowError: Data too large. No Micro QR Code can handle the provided data

Version

It’s possible to specify the desired version for the provided content.

>>> import segno
>>> qr = segno.make('Light My Fire')
>>> qr.version
'M4'
>>> qr = segno.make('Light My Fire', version=1)
>>> qr.version
1

Error Correction Level

By default, Segno uses the error correction level “L” to encode the (Micro) QR Code. Use the parameter error to change the error correction level. The error parameter is case-insensitive; to specify the error correction level “L”, “l” and “L” are valid values. Available error correction levels are L (lowest error correction level), M, Q and H. The error correction level “H” is not available for Micro QR Codes, if the user specifies the error correction level “H”, a QR Code is generated by make, never a Micro QR Code.

>>> import segno
>>> qr = segno.make('Parisienne Walkways', error='l')  # Explicit error correction level
>>> qr.version
2
>>> # Enhancing the error correction level may enforce another QR Code version
>>> qr = segno.make('Parisienne Walkways', error='H')
>>> qr.version
3

Data Masking

Segno chooses by default the optimal mask for the provided input, but the user may specify the preferred mask as well.

>>> import segno
>>> qr = segno.make('Ai Du')
>>> qr.mask
0
>>> qr = segno.make('Ai Du', mask=3)
>>> qr.mask
3