AskHandle Blog
How to Create a QR Code with Python

How to Create a QR Code with Python
Creating QR codes can be super fun, especially when you can do it using Python! Whether you want to generate a QR code for your website, WiFi network, or a cool little project, Python makes it easy and enjoyable. Let’s dive into the nitty-gritty of generating QR codes with Python step by step.
What is a QR Code?
Before jumping into the coding part, let’s get to know a bit about QR codes. Quick Response (QR) codes are two-dimensional barcodes that can store data such as URLs, emails, phone numbers, and even more. These codes are perfect for quickly sharing information and can be scanned by mobile devices.
Getting Started
To create QR codes with Python, there’s a fantastic library named qrcode that makes the whole process seamless. This library is easy to use and handles QR code generation beautifully.
Setting Up Your Environment
First, make sure you have Python installed on your machine. If not, download and install it from python.org.
Next, you need to install the qrcode library. Open your command prompt or terminal and type:
1pip install qrcode[pil]Adding [pil] ensures that you use the PIL (Python Imaging Library) to generate QR codes as images.
Your First QR Code
Let’s write a simple program to create a QR code. Open your favorite text editor or an Integrated Development Environment (IDE) and create a new file called create_qr.py.
Now, add the following code to your file:
1import qrcode
2
3# Data to be encoded
4data = "https://www.python.org"
5
6# Creating an instance of QRCode
7qr = qrcode.QRCode(
8 version=1, # controls the size of the QR Code
9 error_correction=qrcode.constants.ERROR_CORRECT_L, # controls the error correction
10 box_size=10, # controls how many pixels each "box" of the QR code is
11 border=4, # controls how many boxes thick the border should be
12)
13
14# Add data to the instance
15qr.add_data(data)
16qr.make(fit=True)
17
18# Create an image from the QR Code instance
19img = qr.make_image(fill='black', back_color='white')
20
21# Save the image
22img.save("python_qr.png")In this code:
- We import the
qrcodelibrary. - We provide the data (URL of the Python website) to encode in the QR code.
- We create an instance of the
QRCodeclass with some parameters like version, error correction level, box size, and border. - We add data to the QR code instance and create a QR code image.
- Finally, we save the QR code image as
python_qr.png.
Run this script by typing python create_qr.py in your command prompt or terminal. If everything goes well, you should see a new image file named python_qr.png in your directory!
Customizing Your QR Code
Want to customize your QR codes with different colors and styles? The qrcode library provides flexibility for customization. Let’s give it a try.
Here’s an example of creating a colorful QR code:
1import qrcode
2from PIL import Image
3
4# Data to be encoded
5data = "https://www.python.org"
6
7# Creating an instance of QRCode
8qr = qrcode.QRCode(
9 version=1,
10 error_correction=qrcode.constants.ERROR_CORRECT_H,
11 box_size=10,
12 border=4,
13)
14
15# Add data to the instance
16qr.add_data(data)
17qr.make(fit=True)
18
19# Create an image from the QR Code instance with custom colors
20img = qr.make_image(fill_color="blue", back_color="yellow")
21
22# Save the image
23img.save("python_color_qr.png")In this script, we use fill_color and back_color parameters to change the color of the QR code. Try changing these colors to see what fun combinations you can create!
Embedding Logos in Your QR Code
How cool would it be to embed a logo in your QR code? Let’s jazz up our QR code by embedding a logo.
Firstly, ensure you have a logo image. We’ll use the Python logo for this example. Download the Python logo from Python Logo and save it as python_logo.png.
Now, update your script to include the logo:
1import qrcode
2from PIL import Image
3
4# Data to be encoded
5data = "https://www.python.org"
6
7# Creating an instance of QRCode
8qr = qrcode.QRCode(
9 version=1,
10 error_correction=qrcode.constants.ERROR_CORRECT_H,
11 box_size=10,
12 border=4,
13)
14
15# Add data to the instance
16qr.add_data(data)
17qr.make(fit=True)
18
19# Create an image from the QR Code instance
20img = qr.make_image(fill_color="blue", back_color="white")
21
22# Load the logo
23logo = Image.open('python_logo.png')
24
25# Calculate the size of the logo
26base_width = 50
27wpercent = (base_width / float(logo.size[0]))
28h_size = int((float(logo.size[1]) * float(wpercent)))
29logo = logo.resize((base_width, h_size), Image.ANTIALIAS)
30
31# Calculate the position to paste the logo
32pos = ((img.size[0] - logo.size[0]) // 2, (img.size[1] - logo.size[1]) // 2)
33img.paste(logo, pos, mask=logo)
34
35# Save the final QR code with the logo embedded
36img.save("python_logo_qr.png")This script:
- Loads the QR code image.
- Loads and resizes the logo to fit within the QR code.
- Calculates the position to center the logo within the QR code.
- Pastes the logo onto the QR code and saves the result.
Run the script, and check out your stylish QR code with the Python logo embedded in it!
Creating QR codes with Python is not just easy but also offers a lot of room for customization. With the qrcode library, you can generate simple QR codes, add colors, and even embed logos. Whether you’re working on a personal project or looking to share information creatively, QR codes are a fantastic tool to have in your toolkit. Try experimenting with different data and customization options to see what incredible QR codes you can make!