PyQRCode Builder Documentation

This module does the actual generation of the QR codes. The QRCodeBuilder builds the code. While the various output methods draw the code into a file.

class pyqrcode.builder.QRCodeBuilder(data, version, mode, error)[source]

This class generates a QR code based on the standard. It is meant to be used internally, not by users!!!

This class implements the tutorials found at:

This class also uses the standard, which can be read online at:
Test codes were tested against:
Also, reference codes were generat/ed at:
QR code Debugger:
__init__(data, version, mode, error)[source]

See pyqrcode.QRCode for information on the parameters.


This function properly constructs a QR code’s data string. It takes into account the interleaving pattern required by the standard.


This method add the detection patterns to the QR code. This lets the scanner orient the pattern. It is required for all QR codes. The detection pattern consists of three boxes located at the upper left, upper right, and lower left corners of the matrix. Also, two special lines called the timing pattern is also necessary. Finally, a single black pixel is added just above the lower left black box.


This method draws the position adjustment patterns onto the QR Code. All QR code versions larger than one require these special boxes called position adjustment patterns.

add_type_pattern(m, type_bits)[source]

This will add the pattern to the QR code that represents the error level and the type of mask used to make the code.


For QR codes with a version 7 or higher, a special pattern specifying the code’s version is required.

For further information see:


The data block must fill the entire data capacity of the QR code. If we fall short, then we must add bytes to the end of the encoded data field. The value of these bytes are specified in the standard.

binary_string(data, length)[source]

This method returns a string of length n that is the binary representation of the given data. This function is used to basically create bit fields of a given size.


This method returns the index of the “best” mask as defined by having the lowest total penalty score. The penalty rules are defined by the standard. The mask with the lowest total score should be the easiest to read by optical scanners.


This method takes the existing encoded binary string and returns a binary string that will pad it such that the encoded string contains only full bytes.


This method encodes the data into a binary string using the appropriate algorithm specified by the mode.


This method encodes the QR code’s data if its mode is alphanumeric. It returns the data encoded as a binary string.


This method encodes the QR code’s data if its mode is 8 bit mode. It returns the data encoded as a binary string.


This method encodes the QR code’s data if its mode is kanji. It returns the data encoded as a binary string.


This method encodes the QR code’s data if its mode is numeric. It returns the data encoded as a binary string.


QR codes contain a “data length” field. This method creates this field. A binary string representing the appropriate length is returned.

grouper(n, iterable, fillvalue=None)[source]

This generator yields a set of tuples, where the iterable is broken into n sized chunks. If the iterable is not evenly sized then fillvalue will be appended to the last tuple to make up the difference.

This function is copied from the standard docs on itertools.


This method returns the best possible QR code.

make_error_block(block, block_number)[source]

This function constructs the error correction block of the given data block. This is very complicated process. To understand the code you need to read:


This method generates all seven masks so that the best mask can be determined. The template parameter is a code matrix that will server as the base for all the generated masks.


This method adds zeros to the end of the encoded data so that the encoded data is of the correct length. It returns a binary string containing the bits to be added.