Python Examples
Convert IP to Integer and Integer To IP in Python How to Get IPv4/IPv6 Address Range from CIDR in Python? Compare Two Objects For Equality in Python How to find Duplicate Elements from a List in Python Convert Timestamp to datetime in Python Convert datetime to Timestamp in Python Generate Random String of Specific Length in Python Encryption and Decryption of Strings in Python The string module in Python Convert string to bytes in Python Convert bytes to string in Python Convert string to datetime and datetime to string in Python Call a function Asynchronously from within a Loop and wait for the Results in Python Remove Duplicate Elements from a List in Python Caching in Python with Examples How to Bulk Insert and Retrieve Data from Redis in Python How to Write Unit Test in Python Read and Write CSV Files in Python Read and Write Data to a Text File in Python How to Convert CSV to JSON in Python Create ICS Calendar File in Python Install Python on Windows 10/11 Install Python on Ubuntu 20.04 or 22.04.3 Python - Install Virtual Environment How to Find a Specific Field Value from a JSON list in Python Download and Unzip a Zipped File in Python Python Install PIP Python Install Virtual Environment How to Fix Python Error: string argument without an encoding Compare Two JSON files in Python How to Hash a Dictionary Object in Python? Create a Digital Clock in Python Create Multiple URLs Using Each Path of a URL in Python Send an Email with Multiple Attachments using Amazon SES in Python SQLAlchemy Query Examples for Effective Database Management SQLAlchemy Query to Find IP Addresses from an IP Range in Bulk How to Create and Use Configuration files in a Python Project Check if a Value Already Exists in a List of Dictionary Objects in Python How to Split Large Files by size in Python? Fixing - Running Scripts is Disabled on this System Error on Windows Generating QR Codes in Python Reading QR Codes in Python

Generating QR Codes in Python

  • Last updated Apr 25, 2024

QR Codes in Python can be generated using the qrcode library. Follow the steps below to generate a QR code:

Install the qrcode library

If you haven't already installed the qrcode library, you can install it using pip:

pip install qrcode[pil]
Generate QR Code

Here's a simple example that demonstrates how to generate a QR code for a given text:

import qrcode

def generate_qr_code(text, filename, version=1, error_correction=ERROR_CORRECT_L, box_size=10, border=4):
    # Create QR code instance
    qr = qrcode.QRCode(

    # Generate QR code image
    img = qr.make_image(fill='black', back_color='white')

    # Save the QR code image

if __name__ == "__main__":
    text_to_encode = "Hello, QR Code Buddy!"
    output_filename = "qrcode.png"
    # Available error correction levels:
    # ERROR_CORRECT_L: About 7% or fewer errors can be corrected.
    # ERROR_CORRECT_M: About 15% or fewer errors can be corrected.
    # ERROR_CORRECT_Q: About 25% or fewer errors can be corrected.
    # ERROR_CORRECT_H: About 30% or fewer errors can be corrected.
    error_correction_level = ERROR_CORRECT_L
    generate_qr_code(text_to_encode, output_filename, version=2, error_correction=error_correction_level, box_size=14, border=2)
    print(f"QR Code generated and saved as {output_filename}")

Execute the Python script. After running, you should find a qrcode.png file in your current directory containing the generated QR code. You can customize the QR code's version (size), error correction level, box size, and border by passing the desired values to the generate_qr_code function. Adjust the parameters as needed for your specific requirements.