Python으로 QR Code 생성하기

<\/script>\n
'; }, get iframeSnippet() { const domain = 'qrcodefyi.com'; const type = 'guide'; const slug = 'python-qr-generation'; return ''; }, get activeSnippet() { return this.method === 'script' ? this.scriptSnippet : this.iframeSnippet; }, copySnippet() { navigator.clipboard.writeText(this.activeSnippet).then(() => { this.copied = true; setTimeout(() => { this.copied = false; }, 2000); }); } }" @keydown.escape.window="open = false" @click.outside="open = false">

Embed This Widget

Theme


      
    

Widget powered by . Free, no account required.

Python QR code generation tutorial: qrcode and segno libraries, error correction, SVG/PNG output, and Django integration.

Generating QR Codes in Python

Python offers excellent QR code generation libraries. This guide covers practical usage with qrcode and segno, including Django integration.

Using the qrcode Library

import qrcode

qr = qrcode.QRCode(
    version=None,  # Auto-select
    error_correction=qrcode.constants.ERROR_CORRECT_M,
    box_size=10,
    border=4,
)
qr.add_data("https://example.com")
qr.make(fit=True)

img = qr.make_image(fill_color="black", back_color="white")
img.save("qrcode.png")

Using segno

import segno

qr = segno.make("https://example.com", error="m")
qr.save("qrcode.svg", scale=10)
qr.save("qrcode.png", scale=10)

segno is faster and produces smaller files, especially for SVG output. It also supports Micro QR generation.

Django Integration

Serve QR codes as dynamic images in Django views:

import io
import segno
from django.http import HttpResponse

def qr_code_view(request, data):
    qr = segno.make(data, error="m")
    buffer = io.BytesIO()
    qr.save(buffer, kind="png", scale=8)
    return HttpResponse(buffer.getvalue(), content_type="image/png")

Customisation with qrcode

The qrcode library supports visual customisation via StyledPilImage:

Encoding Different Data Types

# WiFi
segno.helpers.make_wifi(ssid="MyNetwork", password="secret", security="WPA")

# vCard
segno.helpers.make_vcard(name="Doe;John", phone="+15550123")

# Email
segno.make("mailto:[email protected]?subject=Hello")

# Geographic location
segno.make("geo:37.7749,-122.4194")

Performance Considerations

For high-volume generation (API endpoints, batch processing):

  • segno is 2-5x faster than qrcode for generation
  • Pre-compute QR matrices and cache them if the same data is generated repeatedly
  • For SVG output, skip pillow entirely (segno outputs SVG natively)
  • Consider server-side generation with caching for web applications

Key Takeaways

  • segno: fast, standards-compliant, Micro QR support, SVG-native
  • qrcode: flexible styling, logo embedding, pillow integration
  • Django integration: serve QR codes as dynamic image responses
  • Use segno helpers for structured data types (WiFi, vCard)
  • Cache generated QR codes for high-traffic endpoints