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.
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')import segno
qr = segno.make('https://qrcode.fun')
qr.save('qrcode.svg', scale=10)
qr.save('qrcode.png', scale=10, dark='#1A2B3C')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.
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.