🗂 Category: PROGRAMMING
🕒 Date: 2025-12-02 | ⏱️ Read time: 7 min read
---
QR (Quick Response) codes are two-dimensional barcodes that have become ubiquitous for sharing information quickly, from website URLs to Wi-Fi credentials. With Python, generating your own QR codes is a straightforward process, thanks to powerful and easy-to-use libraries.
This tutorial will guide you through using the
qrcode package to create both simple and customized QR codes. By the end, you'll be able to integrate QR code generation into any of your Python projects.#### What You'll Learn:
• How to install the necessary Python package.
• How to create a basic QR code with a single line of code.
• How to use the
QRCode class for advanced customization, including size, border, error correction, and color.---
Step 1: Installation
To get started, you need to install the
qrcode library. This package also requires an image processing library to create and save the QR code as an image file. We'll use Pillow, which can be installed along with the main package.Open your terminal or command prompt and run the following command:
pip install "qrcode[pil]"
This command installs both the
qrcode library and the Pillow dependency, ensuring you have everything you need to generate and save image files.---
Step 2: Creating a Simple QR Code
The quickest way to generate a QR code is by using the
make() function. This is perfect for when you need a standard QR code without any special configurations.Let's create a QR code that encodes the URL to the official Python website.
• Create a new Python file (e.g.,
create_qr.py).• Add the following code:
import qrcode
# The data you want to encode in the QR code
data = "https://www.python.org"
# Generate the QR code image
# The make() function handles the entire process
img = qrcode.make(data)
# Save the generated image to a file
img.save("basic_python_qr.png")
print("QR code generated successfully and saved as 'basic_python_qr.png'")
When you run this script, it will create a file named
basic_python_qr.png in the same directory. If you scan this QR code with your phone, it will take you to the Python website.---
Step 3: Advanced Customization
For more control over the appearance and functionality of your QR code, you can use the
QRCode class. This allows you to configure several parameters:•
version: An integer from 1 to 40 that controls the size of the QR Code. A larger version number means the code can hold more data.•
error_correction: Controls how much of the QR code can be damaged or obscured while still being readable. There are four levels:ERROR_CORRECT_L: About 7% or less errors can be corrected.ERROR_CORRECT_M (default): About 15% or less.ERROR_CORRECT_Q: About 25% or less.ERROR_CORRECT_H: About 30% or less.•
box_size: Controls how many pixels each "box" of the QR code is.•
border: Controls the thickness of the white border around the code.Let's create a customized QR code with a blue color, a thicker border, and high error correction.