How to Generate QR Codes in Python
A QR code (quick-response code) is a type of two-dimensional matrix barcode. It uses four standardized modes of encoding: numeric, alphanumeric, byte or binary, and kanji. Its highest storage capacity can be 2,953 bytes.
Here is a table about different modes:
Input mode | Mode indicator | Max. characters | Possible characters, default encoding |
---|---|---|---|
Numeric only | 1 | 7,089 | 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 |
Alphanumeric | 2 | 4,296 | 0–9, A–Z (upper-case only), space, $, %, *, +, -, ., /, : |
Binary/byte | 4 | 2,953 | ISO/IEC 8859-1 |
Kanji/kana | 8 | 1,817 | Shift JIS X 0208 |
Structured Append | 3 | unlimited | Not specific |
PS: structured append is a mode in which the data is divided into several barcodes.
In this article, we are going to talk about how to generate QR codes in different modes in Python.
Generate QR Codes in Different Modes
We are going to use the segno library to generate QR codes as this is the only library that supports structured append.
-
Numeric QR Codes
qrcode = segno.make_qr("9780593230060",mode="numeric")
-
Alphanumeric QR Codes
qrcode = segno.make_qr("DYNAMSOFT",mode="alphanumeric")
-
Kanji QR Codes
qrcode = segno.make_qr("ディナムソフト",mode="kanji")
Shift-JIS will be used for encoding Kanji.
-
Bytes QR Codes
We can use bytes to store a UTF-8 encoded string.
qrcode = segno.make_qr("Dynamsoft",mode="byte")
We can also directly store the raw bytes of an image file.
f = open("1-bit.png",mode="rb") qrcode = segno.make_qr(f.read(),mode="byte")
-
Structured Append QR Codes
If the content we need to store is larger than one QR code’s capacity or the version of the QR code will be too high if we store it in one QR code which makes it difficult to print and read, we can store the content in multiple QR codes. This can be done using the structured append mode.
f = open("8-bit.png",mode="rb") qrcode_seq = segno.make_sequence(f.read(), symbol_count=2)
Read the QR Codes
You can use this Web QR Code Scanner based on Dynamsoft Barcode Reader to read the QR Codes we generated in this article.
Source Code
Check out the source code to have a try: