QR Code Generator using Python

Related Posts

Introduction

In today’s digital age, QR codes have become ubiquitous. They are found on advertisements, product packaging, event tickets, and more. QR codes provide a quick and easy way to share information and direct users to websites, apps, and other online content. In this tutorial, you will learn how to generate a QR code using Python with the `qrcode` library. This is a straightforward process that involves creating a QR code for a specific URL and saving it as an image file.

Why Use QR Codes?

QR codes are a powerful tool for both businesses and individuals. Here are some reasons why you might want to use QR codes:

  • Easy to Use: QR codes can be scanned quickly with a smartphone, providing instant access to information.
  • Versatile: They can be used for various purposes, including linking to websites, providing contact information, or sharing promotions.
  • Cost-Effective: Generating QR codes is free, making them an affordable marketing tool.

Prerequisites

To follow along with this tutorial, you will need:

  • Basic knowledge of Python programming.
  • The `qrcode` library installed on your machine.

You can install the `qrcode` library using pip:

pip install qrcode[pil]

Script Overview

The script will perform the following steps:

  1. Import the necessary library.
  2. Create a function to generate the QR code.
  3. Specify the URL and file path for the QR code image.
  4. Generate and save the QR code image.

Step-by-Step Guide

Step 1: Import Necessary Library

import qrcode

Step 2: Create the QR Code Generation Function

The function `generate_qr_code` takes a URL and a file path as inputs. It creates a QR code for the specified URL and saves it as an image file.

def generate_qr_code(url, file_path):
    # Create a QR code instance with the specified data
    qr = qrcode.QRCode(
        version=1,  # controls the size of the QR Code
        error_correction=qrcode.constants.ERROR_CORRECT_L,  # controls the error correction used for the QR Code
        box_size=10,  # controls how many pixels each “box” of the QR code is
        border=4,  # controls how many boxes thick the border should be
    )

    qr.add_data(url)
    qr.make(fit=True)

    # Create an QR code image with specific color and background color
    img = qr.make_image(fill='black', back_color='white')
    img.save(file_path)
    print(f"QR Code saved at {file_path}")

Step 3: Specify the URL and File Path

Define the URL you want to encode in the QR code and the file path where the image will be saved.

# Example usage for KD Jayakody Blog
url = "https://blog.kdj.lk/"
file_path = "kdj_qr_code.png"

Step 4: Generate and Save the QR Code Image

Call the `generate_qr_code` function with the specified URL and file path.

generate_qr_code(url, file_path)

Conclusion

By following these steps, you can easily generate QR codes for any URL using Python. QR codes are a convenient and effective way to share information, and this script can be customized to fit your specific needs. Whether you’re directing users to your blog, sharing contact information, or promoting an event, QR codes make it easy for people to access the information they need.