How to Generate QR Codes Using Python

QR codes are useful for sharing website links, text, contact details, and other information. You can scan them using a smartphone camera to access the stored information.

In this tutorial, we’ll learn how to create a simple QR code generator using Python. We’ll ask the user to enter some text or a website URL, generate a QR code, and save it as a PNG image.

We’ll use the Python qrcode package to generate the QR code. You don’t need advanced Python knowledge to follow this tutorial.

Prefer watching a video tutorial?

If you prefer learning by watching, check out my YouTube tutorial. I’ll walk you through the process of creating a QR code generator using Python.

Watch the tutorial on YouTube

How to Generate QR Codes Using Python

1. Create a Project Folder

First, create a new folder named qr. We’ll use this folder to store our Python file and the generated QR code image.

Open this folder in your code editor and create a new Python file named main.py.

Our project structure will look like this:

qr/
└── main.py

2. Install the Required Python Package

Before writing the code, we need to install a Python package called qrcode.

This package helps us generate QR codes from text or URLs. We’ll also install Pillow, which is used to create and save the QR code image.

The installation command depends on your operating system.

For macOS

Open the terminal inside your project folder.

First, create a virtual environment:

python3 -m venv qr-env

A virtual environment keeps the packages for this project separate from other Python projects.

Activate the virtual environment using:

source qr-env/bin/activate

Now, install the required package:

pip3 install "qrcode[pil]"

For Windows

Open Command Prompt or PowerShell and run:

pip install "qrcode[pil]"

For Linux

Open the terminal and run:

pip3 install "qrcode[pil]"

Once the package is installed, we’re ready to write our Python code.

3. Import the Required Modules

Open main.py and import the required modules.

import qrcode
import os

Here, we’re importing two modules:

  • qrcode: We’ll use this package to generate QR codes.
  • os: We’ll use this module to create the file path where the generated QR code image will be saved.

4. Get Input from the User

Now, we need some text or a website URL to generate the QR code.

We’ll use Python’s input() function to get this information from the user.

text = input("Enter URL or text: ").strip()

The input() function displays a message and waits for the user to enter something.

Whatever the user enters is stored in the text variable.

We’ve also added the strip() method. It removes extra spaces from the beginning and end of the entered text.

For example, if someone enters a URL with extra spaces, Python will remove those spaces before generating the QR code.

5. Check Whether the Input Is Empty

Before generating the QR code, let’s check whether the user has entered any text.

if not text:
    print("Nothing entered. Please run the script again.")
    exit()

Here, if not text checks whether the text variable is empty.

If the user presses Enter without entering anything, Python displays a message and stops the program using the exit()function.

If the user enters some text or a URL, the program skips this condition and continues.

6. Generate the QR Code

Now comes the main part of our program.

We’ll use the make() method from the qrcode package to generate the QR code.

img = qrcode.make(text)

The qrcode.make() method takes the text or URL stored in the text variable and generates a QR code.

The generated image is stored in the img variable.

We’ll use this variable in the next step to save the QR code as an image.

7. Set the Image Saving Path

Now, let’s decide where to save the generated QR code.

In this example, we’ll save the image inside our project folder.

First, define the folder path:

desktop = os.path.expanduser("/Volumes/DevDrive/python_proj/qr")

Note: This is the folder path from my local Mac. You should replace it with the actual path of your own project folder.

Next, define the image filename:

save_path = os.path.join(desktop, "qrcode.png")

The os.path.join() function combines the folder path and filename.

So, our generated image will be saved as qrcode.png inside the qr folder.

8. Save the QR Code Image

We’ve generated the QR code and defined where to save it.

Now, let’s save the image using the save() method.

img.save(save_path)

The save() method saves the generated QR code image to the specified location.

We can also display a message to let the user know where the image was saved.

print(f"QR code saved: {save_path}")

Here, we’re using an f-string. It allows us to insert the value of a variable directly into a string.

The program will display the complete image path after generating the QR code.

9. Complete Python Code

We’ve completed all the steps. Here is the full code for our Python QR code generator.

Create or open main.py and add the following code:

import qrcode
import os

# Get input from the user
text = input("Enter URL or text: ").strip()

# Check for empty input
if not text:
    print("Nothing entered. Please run the script again.")
    exit()

# Generate QR code
img = qrcode.make(text)

# Set image saving path
desktop = os.path.expanduser("/Volumes/DevDrive/python_proj/qr")
save_path = os.path.join(desktop, "qrcode.png")

# Save QR code image
img.save(save_path)

print(f"QR code saved: {save_path}")

Remember to replace the folder path with your own project folder path before running the program.

10. Run and Test the Program

Now, let’s test our QR code generator.

Open the terminal inside your project folder and run:

python3 main.py

On Windows, you can use:

python main.py

The program will ask you to enter a URL or text.

Let’s first enter a website URL:

https://makitweb.com

Press Enter.

Python will generate the QR code and display the location where the image was saved.

Open your project folder. You’ll find a new image named qrcode.png.

Open this image and scan it using your smartphone. It should open the website you entered.

You can also test the program with normal text instead of a URL.

Run the script again and enter a message, for example:

Hello, welcome to Makitweb!

Python will generate another QR code. When you scan it using your phone, you’ll see the entered message.

One thing to remember: The generated image always uses the same filename, qrcode.png. So, when you run the program again, the previous image will be replaced with the new one.

Conclusion

In this tutorial, we learned how to generate QR codes using Python and the qrcode package.

We created a simple program that accepts text or a website URL from the user, generates a QR code, and saves it as a PNG image.

You can use the same approach to create QR codes for different types of text and website links.

Keep practising and try adding more features to this project as you learn Python.

Watch the complete video tutorial

Want to see the code in action? Watch the complete tutorial on YouTube.

Leave a Comment