🔥 Build Your Own QR Code Generator in Python
Want to create your own QR code generator in Python? Whether you want to encode a website, contact info, or Wi-Fi details, you’re in the right place. With just a few lines of code, you’ll have a fully working QR code generator — and it’ll only take you 5 minutes or less. Let’s go! 💻📱
What You’ll Need
Python 3.x installed
A terminal or code editor (like VS Code)
qrcode
andpillow
libraries
Install them by running:
pip install qrcode[pil]
That’s it. You’re ready to go!
🧠 How It Works
We’ll use the qrcode
library to generate a QR code from any text or URL and save it as an image file.
📝 Step-by-Step Code
import qrcode
# Step 1: Enter the data you want to encode
data = input(“Enter the text or URL to encode in QR code: “)
# Step 2: Generate QR code
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
# Step 3: Create and save the QR image
img = qr.make_image(fill_color=”black”, back_color=”white”)
img.save(“my_qr_code.png”)
print(“✅ QR Code generated and saved as ‘my_qr_code.png'”)
🧪 Example Output
Just run the script, type in a URL like https://example.com
, and boom — it saves a QR code image you can scan instantly.
🎁 BONUS: Build a GUI QR Code Generator with Color & Logo Support!
Want to take this project up a notch? Let’s turn this into a user-friendly desktop app with GUI, complete with color pickers and logo embedding. Perfect for branding or just flexing your Python skills!
🧩 You’ll Need:
pip install pillow
We’ll also use tkinter
, which comes pre-installed with Python.
🎨 GUI Code with Logo + Color Picker
import qrcode
from PIL import Image, ImageTk
import tkinter as tk
from tkinter import filedialog, colorchooser, messagebox
def generate_qr():
data = entry.get()
if not data:
messagebox.showwarning(“Input required”, “Please enter text or URL!”)
return
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
qr_img = qr.make_image(fill_color=fill_color.get(), back_color=back_color.get()).convert(“RGB”)
# Add logo if selected
if logo_path.get():
try:
logo = Image.open(logo_path.get())
qr_w, qr_h = qr_img.size
# Resize logo
logo_size = int(qr_w / 4)
logo = logo.resize((logo_size, logo_size))
pos = ((qr_w – logo_size) // 2, (qr_h – logo_size) // 2)
qr_img.paste(logo, pos, mask=logo if logo.mode == ‘RGBA’ else None)
except Exception as e:
messagebox.showerror(“Logo Error”, f”Failed to add logo: {e}”)
filename = filedialog.asksaveasfilename(defaultextension=”.png”, filetypes=[(“PNG files”, “*.png”)])
if filename:
qr_img.save(filename)
messagebox.showinfo(“Success”, f”QR Code saved as {filename}”)
def choose_color(var):
color_code = colorchooser.askcolor(title=”Choose color”)[1]
if color_code:
var.set(color_code)
def choose_logo():
path = filedialog.askopenfilename(filetypes=[(“Image files”, “*.png;*.jpg;*.jpeg”)])
if path:
logo_path.set(path)
# GUI
root = tk.Tk()
root.title(“QR Code Generator”)
tk.Label(root, text=”Enter text or URL:”).pack(pady=5)
entry = tk.Entry(root, width=40)
entry.pack(pady=5)
fill_color = tk.StringVar(value=”black”)
back_color = tk.StringVar(value=”white”)
logo_path = tk.StringVar(value=””)
tk.Button(root, text=”Pick Fill Color”, command=lambda: choose_color(fill_color)).pack(pady=2)
tk.Button(root, text=”Pick Background Color”, command=lambda: choose_color(back_color)).pack(pady=2)
tk.Button(root, text=”Choose Logo (optional)”, command=choose_logo).pack(pady=2)
tk.Button(root, text=”Generate QR Code”, command=generate_qr, bg=”green”, fg=”white”).pack(pady=10)
root.mainloop()
And you’re good to go!
🖼️ What You Get
✅ Fully functioning desktop app
🎨 Custom color support
📷 Optional logo embed
📁 Save to PNG
🏁 Final Thoughts
You’ve just created not one but two QR code generators — a quick script and a powerful GUI-based app. Whether you’re a beginner or looking to enhance your Python project portfolio, this is a fun and practical build to have under your belt.
💬 Got questions or want to add features like QR previews or batch generation? Drop a comment or reach out! And watch the following video.