QR Code Generator using Python

Published on:

In this post, we will create a simple QR Code generator using Python. QR Codes (Quick Response Codes) are two-dimensional barcodes that can store a variety of information, such as URLs, text, or contact information. With just a quick scan using a smartphone camera, users can access the embedded information.

Prerequisites

Before we start coding, make sure you have the following:

  1. Python Installed: Ensure that you have Python installed on your machine. You can download it from python.org.
  2. Libraries: We will use the qrcode and Pillow libraries. You can install these libraries using pip. Open your terminal or command prompt and run the following command:
pip install qrcode[pil]

Code for QR Code Generator using Python

Below is the complete code for generating a QR Code:

import qrcode
from PIL import Image

def generate_qr_code(data, filename):
    # Create a QR Code instance
    qr = qrcode.QRCode(
        version=1,  # Controls the size of the QR Code
        error_correction=qrcode.constants.ERROR_CORRECT_L,  # Error correction level
        box_size=10,  # Size of each box in pixels
        border=4,  # Thickness of the border (minimum is 4)
    )

    # Add data to the QR Code
    qr.add_data(data)
    qr.make(fit=True)  # Fit the QR Code to the data

    # Create an image from the QR Code instance
    img = qr.make_image(fill_color="black", back_color="white")

    # Save the image to a file
    img.save(filename)
    print(f"QR Code generated and saved as {filename}")

if __name__ == "__main__":
    # Data to be encoded in the QR Code
    data = "https://getprojects.org"
    
    # Filename for the generated QR Code image
    filename = "qrcode.png"
    
    # Generate the QR Code
    generate_qr_code(data, filename)

Code Breakdown – QR Code Generator using Python

  1. Importing Libraries: We import the qrcode library for generating QR Codes and Image from Pillow for image handling.
  2. Function Definition: The generate_qr_code function takes two parameters: data (the information to encode) and filename (the name of the file to save the QR Code image).
  3. QR Code Configuration:
    • version: Controls the size of the QR Code (1 is the smallest).
    • error_correction: Defines the error correction capability. Higher levels allow the QR Code to be read even if partially damaged.
    • box_size: Determines the size of each individual box in the QR Code.
    • border: Specifies the thickness of the border around the QR Code.
  4. Adding Data: The add_data method is used to embed the specified data into the QR Code.
  5. Creating the Image: The make_image method generates the QR Code image with specified fill and background colors.
  6. Saving the Image: The generated QR Code is saved as a PNG file using the save method.
  7. Main Block: In the if __name__ == "__main__": block, we specify the data (in this case, the URL “https://getprojects.org“) and the filename for the QR Code image. Finally, we call the generate_qr_code function to create the QR Code.
Read also: Simple Chat Application in Python

Conclusion

In this tutorial, we successfully created a QR Code generator using Python. You can customize the data variable to encode different information, such as text or other URLs. This simple tool can be very useful for sharing links, contact information, or any other data that can be encoded in a QR Code.

Feel free to explore more features of the qrcode library and enhance your QR Code generator further! If you have any questions or suggestions, please leave a comment below.

Happy coding!

Related Articles

Related

Leave a Reply

Please enter your comment!
Please enter your name here