Create Stunning QR Codes in Minutes with Python on Google Colab!

Create Stunning QR Codes in Minutes with Python on Google Colab!

QR codes are everywhere—on product packaging, business cards, websites, and even restaurant menus. They’re a quick and efficient way to share information. But did you know you can create your own custom QR codes using Python, and it’s super easy to do in Google Colab? In this article, we’ll walk you through a step-by-step guide to generating QR codes using Python, right in your browser. Let’s dive in!


Why Use Google Colab for QR Code Generation?

Google Colab is a free, cloud-based platform that allows you to write and run Python code without any setup. It’s perfect for beginners and professionals alike. With Colab, you can generate QR codes in just a few clicks, customize them, and even download or share them instantly. No installations, no hassle—just pure productivity!


Step-by-Step Guide to Generating QR Codes in Google Colab

Below, we’ll walk you through the Python code for generating QR codes and explain how to use it in Google Colab.


1. Open Google Colab and Create a New Notebook

  1. Go to Google Colab.

  2. Click on "New Notebook" to create a blank notebook.

  3. You’re ready to start coding!


2. Install the Required Libraries

In the first cell of your Colab notebook, run the following command to install the necessary libraries:

!pip install qrcode[pil]

This installs the qrcode library for generating QR codes and Pillow for image processing. Google Colab will handle the installation in seconds.


3. Import the Required Modules

In the next cell, import the libraries you’ll need:

import qrcode
from PIL import Image
from IPython.display import display
  • qrcode: The main library for generating QR codes.

  • PIL.Image: Handles image creation and manipulation.

  • IPython.display: Displays the QR code directly in the notebook.


4. Define the QR Code Generation Function

Now, let’s define a function called generate_qr_code that will create and display the QR code. Copy and paste the following code into a new cell:

def generate_qr_code(link, fill_color='black', back_color='white'):
    """
    Generates a QR code from the given link and displays it in the notebook.

    :param link: The URL or text to encode in the QR code.
    :param fill_color: The color of the QR code (default is 'black').
    :param back_color: The background color of the QR code (default is 'white').
    """
    # Create a QR code instance
    qr = qrcode.QRCode(
        version=1,  # Controls the size of the QR Code (1 is the smallest, 40 is the largest)
        error_correction=qrcode.constants.ERROR_CORRECT_L,  # Error correction level
        box_size=10,  # Size of each box in the QR code
        border=4,  # Border size around the QR code
    )

    # Add data to the QR code
    qr.add_data(link)
    qr.make(fit=True)

    # Create an image from the QR code instance
    img = qr.make_image(fill_color=fill_color, back_color=back_color)

    # Display the image in the notebook
    display(img)

5. Generate Your QR Code

To generate a QR code, call the generate_qr_code function. Here’s how you can do it:

  1. In a new cell, add the following code to take user input for the link and colors:
# Input the link from the user
link = input("Enter the link to generate QR code: ")

# Optional: Customize the fill color and background color
fill_color = input("Enter the fill color (default is 'black'): ") or 'black'
back_color = input("Enter the background color (default is 'white'): ") or 'white'

# Generate and display the QR code
generate_qr_code(link, fill_color, back_color)
  1. Run the cell. Google Colab will prompt you to enter:

    • The link you want to encode.

    • The fill color (default is black).

    • The background color (default is white).

  2. After entering the details, your custom QR code will be generated and displayed right in the notebook!


6. Download or Share Your QR Code

Once the QR code is generated, you can:

  • Right-click on the image and select "Save image as" to download it.

  • Share the Colab notebook with others so they can generate their own QR codes.


How Does the Code Work?

Here’s a quick breakdown of the key components:

  • QRCode Instance: The qrcode.QRCode class creates a QR code object. You can customize its size, error correction level, and border.

    • version: Controls the size of the QR code (1 is the smallest, 40 is the largest).

    • error_correction: Determines how much of the QR code can be damaged while still being readable. ERROR_CORRECT_L allows for about 7% damage recovery.

    • box_size: Defines the size of each "box" in the QR code.

    • border: Specifies the width of the border around the QR code.

  • Adding Data: The add_data method encodes the provided link or text into the QR code.

  • Creating the Image: The make_image method generates the QR code as an image, with customizable colors.

  • Displaying the Image: The display function shows the QR code directly in the Colab notebook.


Customization Options

This QR code generator is highly flexible. You can:

  • Change the fill color and background color to match your branding.

  • Adjust the box size and border to make the QR code larger or smaller.

  • Use different error correction levels depending on your needs.


Practical Applications

Here are some ways you can use this QR code generator:

  1. Marketing: Share your website, social media profiles, or promotional offers.

  2. Contactless Payments: Generate QR codes for payment links.

  3. Wi-Fi Sharing: Create QR codes that automatically connect users to your Wi-Fi network.

  4. Event Management: Use QR codes for ticketing or check-ins.


Conclusion

Generating QR codes with Python in Google Colab is quick, easy, and highly customizable. With the code provided in this article, you can create QR codes for any purpose, whether personal or professional. The best part? You don’t need to install anything—just open Colab and start coding!

So, what are you waiting for? Open Google Colab, follow the steps, and start creating your own stunning QR codes today!

For Non-Tech You Can directly copy and Paste in Google Colab

!pip install qrcode[pil]

import qrcode
from PIL import Image
from IPython.display import display

def generate_qr_code(link, fill_color='black', back_color='white'):
    """
    Generates a QR code from the given link and displays it in the notebook.

    :param link: The URL or text to encode in the QR code.
    :param fill_color: The color of the QR code (default is 'black').
    :param back_color: The background color of the QR code (default is 'white').
    """
    # Create a QR code instance
    qr = qrcode.QRCode(
        version=1,  # Controls the size of the QR Code (1 is the smallest, 40 is the largest)
        error_correction=qrcode.constants.ERROR_CORRECT_L,  # Error correction level
        box_size=10,  # Size of each box in the QR code
        border=4,  # Border size around the QR code
    )

    # Add data to the QR code
    qr.add_data(link)
    qr.make(fit=True)

    # Create an image from the QR code instance
    img = qr.make_image(fill_color=fill_color, back_color=back_color)

    # Display the image in the notebook
    display(img)

if __name__ == "__main__":
    # Input the link from the user
    link = input("Enter the link to generate QR code: ")

    # Optional: Customize the fill color and background color
    fill_color = input("Enter the fill color (default is 'black'): ") or 'black'
    back_color = input("Enter the background color (default is 'white'): ") or 'white'

    # Generate and display the QR code
    generate_qr_code(link, fill_color, back_color)

Author Credits: https://chemenggcalc.com/