Generate QR Codes with Python
Python offers powerful QR code libraries like qrcode and segno for scripting, automation, and data pipelines. Generate QR codes locally or use the QRCode.fun API for advanced styling.
Installation
Install Python QR code libraries using pip.
pip install qrcode[pil]pip install segnoGenerate QR Codes with Python Libraries
Code examples using popular Python QR code libraries.
Basic QR Code with qrcode
import qrcode
# Simple generation
img = qrcode.make('https://qrcode.fun')
img.save('qrcode.png')
# Advanced with customization
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data('https://qrcode.fun')
qr.make(fit=True)
img = qr.make_image(fill_color='#1A2B3C', back_color='white')
img.save('qrcode_custom.png')SVG Output with segno
import segno
qr = segno.make('https://qrcode.fun')
qr.save('qrcode.svg', scale=10)
qr.save('qrcode.png', scale=10, dark='#1A2B3C')QR Code with Logo
import qrcode
from PIL import Image
qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
qr.add_data('https://qrcode.fun')
qr.make(fit=True)
img = qr.make_image(fill_color='#1A2B3C', back_color='white').convert('RGB')
# Add logo
logo = Image.open('logo.png')
logo_size = img.size[0] // 4
logo = logo.resize((logo_size, logo_size))
pos = ((img.size[0] - logo_size) // 2, (img.size[1] - logo_size) // 2)
img.paste(logo, pos)
img.save('qrcode_logo.png')Generate QR Codes via API in Python
Call the QRCode.fun API from Python to generate styled QR codes with custom colors, shapes, and logos.
import requests
response = requests.post('https://qrcode.fun/api/generate-qr-styled', json={
'data': 'https://qrcode.fun',
'width': 300,
'height': 300,
'type': 'png',
'margin': 10,
'dotsOptions': {
'color': '#1A2B3C',
'type': 'rounded'
},
'cornersSquareOptions': {
'color': '#8564C3',
'type': 'extra-rounded'
},
'backgroundOptions': {
'color': '#FFFFFF'
}
})
result = response.json()
# result['data'] contains the base64 PNG data URL
print(result['data'][:50])Live QR Code Preview
Try generating a QR code with Python right now.
Native Library vs API
Compare using a Python QR code library directly versus the QRCode.fun API.
| Feature | Native Library | QRCode.fun API |
|---|---|---|
| Setup complexity | pip install + Pillow for images | Single HTTP request via requests |
| Customization | Colors, error correction, box size | Full styling: colors, shapes, logos |
| Offline support | Yes | Requires internet |
| Maintenance | Update packages manually | Always up-to-date |
| Output formats | PNG, SVG, EPS, terminal | PNG, SVG |
Python QR Code Use Cases
Common scenarios where Python developers generate QR codes.
Data Science & Reports
Embed QR codes in automated reports, Jupyter notebooks, and data visualizations linking to dashboards or datasets.
Web Applications (Django/Flask)
Generate QR codes server-side in Django or Flask apps for user profiles, tickets, and authentication tokens.
Automation Scripts
Create QR codes in batch processing scripts for inventory labels, asset tracking, and document management.
IoT & Raspberry Pi
Generate QR codes on embedded devices for Wi-Fi setup, device pairing, and configuration sharing.
Python QR Code Ecosystem Deep Dive
Python's rich ecosystem makes it one of the best languages for QR code generation in data science, web apps, and automation.
Django & FastAPI Integration Patterns
In Django, QR code generation fits naturally into the view layer. Create a dedicated view that returns an HttpResponse with image/png content type, or generate base64 data URLs for template embedding. FastAPI users can return StreamingResponse with QR image bytes. Both frameworks benefit from caching generated QR codes with Redis or Django's cache framework to avoid redundant generation for frequently-requested codes.
Jupyter Notebook & Data Science Workflows
Python's Jupyter ecosystem makes QR codes a powerful tool for data scientists. Generate QR codes inline using IPython.display to render them directly in notebook cells. Link QR codes to interactive dashboards built with Plotly Dash or Streamlit. In automated reporting pipelines with tools like papermill, embed QR codes that link to live data sources or companion notebooks for reproducible research.
Pillow, segno & Advanced Image Processing
The qrcode library integrates with Pillow (PIL) for advanced image manipulation — adding logos, applying color gradients, and compositing QR codes onto marketing materials. segno offers a pure-Python alternative that generates compact SVG without Pillow dependency, ideal for server environments with minimal packages. For production systems, segno's EPS output integrates with professional print workflows, while Pillow-based rendering handles dynamic web content.
Frequently Asked Questions
Common questions about generating QR codes with Python.
Start generating QR codes with Python
Use our free online generator or integrate the API into your Python scripts, Django apps, or data pipelines.