QR Code modes

The ISO/IEC 18004 standard defines four modes in order to encode the data as efficiently as possible. If no encoding or mode is provided, Segno tries to find the most efficient encoding / mode.

The mode may be specified by the mode (CLI: --mode or -m) parameter although it is recommended to let Segno decide which mode / encoding should be used.

Numeric mode

The numeric mode is the most efficient way to encode digits. This mode does not cover negative numbers because it does not support the minus sign (or plus sign).

The numeric mode is supported by QR Codes and Micro QR Codes.

Segno detects the numeric mode if the data is provided as string or integer:

>>> import segno
>>> qr = segno.make('64')
>>> qr2 = segno.make(64)
>>> qr.designator
'M1'
>>> qr2.designator
'M1'
>>> qr.mode
'numeric'
>>> qr2.mode
'numeric'
>>> qr == qr2
True
M1 Micro QR Code encoding "64"

Segno chooses by default a minimal QR Code output. Since the numeric mode is supported by Micro QR Codes and QR Codes, Segno chooses a Micro QR Code as most efficient representation.

To enforce a QR Code, use either the factory function segno.make_qr() or set the micro parameter of segno.make() to False:

>>> import segno
>>> qr = segno.make_qr('64')
>>> qr2 = segno.make(64, micro=False)
>>> qr.designator
'1-H'
>>> qr.mode
'numeric'
>>> qr == qr2
True
1-H QR Code encoding "64"

Alphanumeric mode

The alphanumeric mode extends the Numeric mode by various characters. Namely about the upper case letters ABCDEFGHIJKLMNOPQRSTUVWXYZ, a space character ” ” and other letters $%*+-./:.

>>> import segno
>>> qr = segno.make('REVOLUTION NO. 9')
>>> qr.designator
'M4-M'
>>> qr.mode
'alphanumeric'
M4-M Micro QR Code encoding "REVOLUTION NO. 9"

As stated in Numeric mode, Segno tries to find the smallest possible code. To ensure a QR Code (and not a Micro QR Code), use the above mentioned factory functions:

>>> import segno
>>> qr = segno.make_qr('REVOLUTION NO. 9')
>>> qr2 = segno.make('REVOLUTION NO. 9', micro=False)
>>> qr.designator
'1-Q'
>>> qr.mode
'alphanumeric'
1-Q QR Code encoding "REVOLUTION NO. 9"

Lower case characters are not covered by the alphanumeric mode, but by the Byte mode

>>> import segno
>>> qr = segno.make('Revolution No. 9')
>>> qr.mode
'byte'
1-L QR Code encoding "Revolution No. 9"

Kanji mode

Kanji can be encoded compactly and efficiently and requires significantly less space than encoding the characters in UTF-8.

>>> import segno
>>> qr = segno.make('ビートルズ')
>>> qr.designator
'M3-L'
>>> qr.mode
'kanji'
M3-L Micro QR Code encoding "ビートルズ"
>>> import segno
>>> qr = segno.make_qr('ビートルズ')
>>> qr.designator
'1-Q'
>>> qr.mode
'kanji'
1-Q QR Code encoding "ビートルズ"

Byte mode

The byte mode covers all data which cannot be represented by the other modes. Segno tries, according to ISO/IEC 18004, to encode the data with ISO 8859-1. In case the data cannot be represented by ISO 8859-1, UTF-8 is used as fallback.

>>> import segno
>>> qr = segno.make('Turn off your mind relax and float down stream')
>>> qr.designator
'3-L'
>>> qr.mode
'byte'
3-L QR Code encoding "Turn off your mind relax and float down stream"

The byte mode is also supported by Micro QR Code M3 and M4:

>>> import segno
>>> qr = segno.make('Let it be')
>>> qr.designator
'M3-L'
>>> qr.mode
'byte'
M3-L Micro QR Code encoding "Let it be"

Hanzi mode

The Hanzi mode is not covered by ISO/IEC 18004 and should be used with care since it is not widely supported by QR Code readers, although the ZXing project supports decoding QR Codes which utilize the Hanzi mode.

Note

Since this mode is not covered by the ISO standard, Segno tries not to detect Hanzi. The user has to enable it explicitly. Further, the Hanzi mode is not available for Micro QR Codes.

>>> import segno
>>> qr = segno.make('书读百遍其义自现')
>>> qr.designator
'2-M'
>>> qr.mode
'byte'

The QR Code uses the Byte mode because no other mode fits.

2-M QR Code encoding "书读百遍其义自现" in byte mode

To enable Hanzi, provide the mode in the factory function:

>>> import segno
>>> qr = segno.make('书读百遍其义自现', mode='hanzi')
>>> qr.mode
'hanzi'
>>> qr.designator
'1-M'

As shown, the input is encoded much more compact and a 1-M instead of a 2-M QR Code is generated.

1-M QR Code encoding "书读百遍其义自现" in Hanzi mode