Python QRCode Module
The qrcode library allows you to generate QR codes in Python. It uses the Python Imaging Library (PIL) to create images.
Installation
pip install "qrcode[pil]"
Basic Usage
The simplest way to create a QR code is using the make shortcut function.
import qrcode
img = qrcode.make('Some data here')
img.save("some_file.png")
Advanced Usage
For more control, use the QRCode class.
import qrcode
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data('Some data')
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
img.save("advanced_qr.png")
Parameters
- version: An integer from 1 to 40 that controls the size of the QR Code (smallest, version 1, is a 21x21 matrix). Set to
Noneand usefit=Trueto determine this automatically. - error_correction: Controls the error correction used for the QR Code.
ERROR_CORRECT_L: About 7% or less errors can be corrected.ERROR_CORRECT_M: About 15% or less errors can be corrected (default).ERROR_CORRECT_Q: About 25% or less errors can be corrected.ERROR_CORRECT_H: About 30% or less errors can be corrected.
- box_size: How many pixels each "box" of the QR code is.
- border: How many boxes thick the border should be (default is 4, which is the minimum according to the specs).
Creating SVG
You can also create SVG images.
import qrcode
import qrcode.image.svg
factory = qrcode.image.svg.SvgImage
img = qrcode.make('Some data here', image_factory=factory)
img.save("qr.svg")