Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
Python ✦ Beginner ⏱ 30 minutes

⚙ Build a QR Code Generator with Python

Generate QR codes for any URL, text, or data with Python in minutes. You'll learn to create basic QR codes, customise the colours, add a logo in the centre, and batch-generate dozens of QR codes from a CSV file.

🎯 What You’ll Build

A Python script that takes any URL or text and generates a professional QR code image — custom brand colours, logo in the centre, saved as a PNG. Also includes a batch mode that reads a CSV and generates one QR per row.

📋 What You’ll Need

QR codes are everywhere — event tickets, menus, business cards, product labels. Building your own generator means you can create them in bulk, brand them, and automate the whole process. Let's build it.

1

Install the libraries

We need the qrcode library for generating QR codes, and Pillow for image manipulation (adding logos):

pip install qrcode[pil] pillow

The [pil] part installs Pillow automatically alongside qrcode. Run this once and you're set.

2

Generate your first QR code

Create qr_generator.py and generate a basic QR code in 5 lines:

import qrcode

qr = qrcode.make('https://itexperttraining.com')
qr.save('myqr.png')

print('QR code saved as myqr.png')

Run it. Open myqr.png — you'll see a black and white QR code. Scan it with your phone and it opens the URL.

3

Customise colours and size

Use the QRCode class for full control over the output:

import qrcode

qr = qrcode.QRCode(
version=1, # size 1-40, 1 = smallest
error_correction=qrcode.constants.ERROR_CORRECT_H, # 30% damage OK
box_size=10, # pixels per box
border=4, # quiet zone boxes
)
qr.add_data('https://itexperttraining.com')
qr.make(fit=True)

img = qr.make_image(fill_color='#162447', back_color='white')
img.save('branded_qr.png')

fill_color sets the QR dot colour. back_color sets the background. Use any hex colour or colour name. ERROR_CORRECT_H is required for adding a logo — it allows 30% of the QR to be covered.

4

Add a logo in the centre

Place a logo image in the middle of the QR code. The high error correction means it still scans perfectly:

from PIL import Image

# Load your logo (PNG with transparent background works best)
logo = Image.open('logo.png')

# Resize logo to 25% of QR size
qr_w, qr_h = img.size
logo_size = qr_w // 4
logo = logo.resize((logo_size, logo_size))

# Paste logo in the centre
pos = ((qr_w - logo_size) // 2, (qr_h - logo_size) // 2)
img.paste(logo, pos, mask=logo if logo.mode == 'RGBA' else None)
img.save('qr_with_logo.png')
💡 Tip: Use a PNG logo with a transparent background (RGBA mode). The mask=logo argument respects the transparency so the QR background shows through.
5

Create a reusable function

Wrap everything into a clean, reusable function:

import qrcode
from PIL import Image

def make_qr(data, filename, color='#162447', logo_path=None):
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H, box_size=10, border=4)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color=color, back_color='white')
if logo_path:
logo = Image.open(logo_path).resize((img.size[0]//4, img.size[1]//4))
pos = ((img.size[0]-logo.size[0])//2, (img.size[1]-logo.size[1])//2)
img.paste(logo, pos, mask=logo if logo.mode=='RGBA' else None)
img.save(filename)
print(f'Saved: {filename}')

# Use it:
make_qr('https://itexperttraining.com', 'site_qr.png', logo_path='logo.png')
6

Batch generate QR codes from a CSV

If you need dozens of QR codes — product pages, event tickets, business cards — read from a CSV:

import csv, os

# qr_list.csv has columns: name, url
os.makedirs('qr_output', exist_ok=True)

with open('qr_list.csv') as f:
for row in csv.DictReader(f):
filename = f'qr_output/{row["name"].replace(" ", "_")}.png'
make_qr(row['url'], filename)

print('All QR codes generated!')

Your qr_list.csv should look like:

name,url
Homepage,https://itexperttraining.com
Blog,https://itexperttraining.com/learn
Contact,https://itexperttraining.com/contact
Output
Saved: qr_output/Homepage.png
Saved: qr_output/Blog.png
Saved: qr_output/Contact.png
All QR codes generated!
7

Generate a QR for any input from the command line

Make your script accept input from the command line so you can use it like a tool:

import sys

if __name__ == '__main__':
if len(sys.argv) < 3:
print('Usage: python qr_generator.py <data> <output.png>')
sys.exit(1)
make_qr(sys.argv[1], sys.argv[2])

Now run from the terminal:

python qr_generator.py "https://example.com" myqr.png

One command, one QR code. You can use this in shell scripts or scheduled tasks.

🎉

You did it!

You built a complete QR code generator — basic codes, branded colours, logo overlays, and batch CSV generation. This is a genuinely useful tool you can use right now for business cards, events, or product labels.

🚀 Take It Further

← All Tutorials

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.