python-qrcode

repository·main·Indexed 26 days ago

https://github.com/lincolnloop/python-qrcode

A pure Python QR Code generator (version 8.2) capable of creating QR codes in PNG, SVG, and ASCII formats. It provides a high-level `make()` shortcut, a granular `QRCode` class for configuring version, box size, and border, and a `qr` command-line interface. The library supports various error correction levels (L, M, Q, H) and advanced styling via `StyledPilImage`, including rounded modules, gradients, and embedded images.

Tokens
3K
Snippets
8
Records
26
Agent score
88%

What's inside python-qrcode

  1. Install qrcode

    main

    To install the basic version of qrcode (which includes a pure Python PNG encoder using pypng and console rendering support), run:

    pip install qrcode

    For advanced image functionality, including support for Pillow (PIL) to generate high-quality images, install with the pil dependency:

    pip install "qrcode[pil]"
    pip install qrcode
    pip install "qrcode[pil]"
  2. Manage QR Code data with add_data and clear

    main

    The add_data() method appends data to the current QRCode object. If you want to replace the existing content with new data in the same object, you must call clear() first.

    import qrcode
    qr = qrcode.QRCode()
    qr.add_data('Some data')
    img = qr.make_image()
    
    # Replace data
    qr.clear()
    qr.add_data('New data')
    other_img = qr.make_image()
  3. Generate Styled QR codes with StyledPilImage

    main

    For highly customized QR codes (rounded corners, gradients, or embedded images), use StyledPilImage. This requires the pillow dependency.

    Key Features:

    • module_drawer: Controls the shape of the QR modules (e.g., RoundedModuleDrawer, CircleDrawer).
    • color_mask: Changes the colors of the QR code (e.g., RadialGradiantColorMask).
    • embedded_image_path: Path to an image to embed in the center of the QR code.

    Note: Styled QR codes are not guaranteed to work with all readers. It is recommended to use ERROR_CORRECT_H when using styles or embedded images.

    import qrcode
    from qrcode.image.styledpil import StyledPilImage
    from qrcode.image.styles.moduledrawers.pil import RoundedModuleDrawer
    from qrcode.image.styles.colormasks import RadialGradiantColorMask
    
    qr = qrcode.QRCode(error_correction=qrcode.constants.ERROR_CORRECT_H)
    qr.add_data('Some data')
    
    # Example: Rounded corners, radial gradient, and embedded image
    img = qr.make_image(
        image_factory=StyledPilImage, 
        module_drawer=RoundedModuleDrawer(),
        color_mask=RadialGradiantColorMask(),
        embedded_image_path="/path/to/image.png"
    )
    img.save("styled_qr.png")
  4. Generate SVG QR codes in Python

    main

    To generate SVG files in Python, you must specify an image_factory from qrcode.image.svg.

    Available SVG Factories:

    • qrcode.image.svg.SvgImage: Simple factory (set of rectangles).
    • qrcode.image.svg.SvgFragmentImage: Fragment factory (no standalone header).
    • qrcode.image.svg.SvgPathImage: Combined path factory (recommended; fixes white space issues when zooming).
    • qrcode.image.svg.SvgFillImage: SVG factory that fills the background with white.
    • qrcode.image.svg.SvgPathFillImage: SVG path factory that fills the background with white.

    Customizing SVG Attributes:

    The make_image() method forwards keyword arguments to the underlying ElementTree XML library via the attrib parameter.

    import qrcode
    import qrcode.image.svg
    
    # Using the Path factory
    qr = qrcode.QRCode(image_factory=qrcode.image.svg.SvgPathImage)
    qr.add_data('Some data')
    qr.make(fit=True)
    
    # Adding custom XML attributes
    img = qr.make_image(attrib={'class': 'some-css-class'})
    img.save('some_file.svg')
    
    # Converting to string
    svg_string = img.to_string(encoding='unicode')
  5. Advanced QR Code configuration with QRCode class

    main

    For granular control over the QR code properties, use the qrcode.QRCode class.

    Parameters:

    • version: Integer from 1 to 40 (size of the matrix). Set to None and use fit=True in make() to determine automatically.
    • error_correction: Controls error correction capability. Use constants from qrcode.constants:
      • ERROR_CORRECT_L: ~7% error correction.
      • ERROR_CORRECT_M: ~15% error correction (default).
      • ERROR_CORRECT_Q: ~25% error correction.
      • ERROR_CORRECT_H: ~30% error correction.
    • box_size: Controls how many pixels each "box" of the QR code is.
    • border: Controls the thickness of the border (default is 4).

    Customizing Colors:

    When using the default image factory, you can pass fill_color and back_color (accepting RGB tuples) to make_image().

    import qrcode
    qr = qrcode.QRCode(
        version=1,
        error_correction=qrcode.constants.ERROR_CORRECT_L,
        box_size=10,
        border=4,
    )
    qr.add_data('Some data')
    qr.make(fit=True)
    
    # Using RGB tuples for colors
    img = qr.make_image(fill_color=(55, 95, 35), back_color=(255, 195, 235))
    img.save("qrcode.png")
  6. Generate QR codes via CLI

    main

    You can generate QR codes directly from the command line using the qr script.

    Standard PNG output:

    qr "Some text" > test.png

    Using specific output flags (recommended for PowerShell):

    qr --output=test.png "Some data"

    ASCII output:

    qr --ascii "Some data" > "test.txt"

    SVG output with different factories:

    qr --factory=svg-path "Some text" > test.svg
    qr --factory=svg "Some text" > test.svg
    qr --factory=svg-fragment "Some text" > test.svg
    qr "Some text" > test.png
    qr --output=test.png "Some data"
    qr --ascii "Some data" > "test.txt"
    qr --factory=svg-path "Some text" > test.svg