CtrlK
CommunityDocumentationLog inGet started
Tessl Logo

tessl/pypi-qrcode

QR Code image generator with customizable image formats, error correction levels, and styling options

42%

Overall

Evaluation42%

0.86x

Agent success when using this tile

Overview
Eval results
Files

console-interface.mddocs/

Console Interface

Command-line interface and text-based output methods for generating QR codes directly in terminals, saving to files, and displaying QR codes using ASCII characters or TTY colors.

Capabilities

Command Line Tool

The qr command provides full QR code generation from the command line with support for all image formats and output options.

# Basic usage
qr "Hello World" > qrcode.png

# Specify output file
qr "Hello World" --output qrcode.png

# Different image formats
qr "Hello World" --factory svg > qrcode.svg
qr "Hello World" --factory png > qrcode.png
qr "Hello World" --factory pil > qrcode.png

# Error correction levels
qr "Hello World" --error-correction L > qrcode_low.png    # Low (7%)
qr "Hello World" --error-correction M > qrcode_med.png    # Medium (15%, default)
qr "Hello World" --error-correction Q > qrcode_q.png      # Quartile (25%)
qr "Hello World" --error-correction H > qrcode_high.png   # High (30%)

# ASCII output to terminal
qr "Hello World" --ascii

# Read from stdin
echo "Hello World" | qr > qrcode.png
cat data.txt | qr --output qrcode.png

Main Function

The main entry point for the command-line interface.

from qrcode.console_scripts import main

def main(args=None):
    """
    Main entry point for the qr command-line tool.
    
    Parameters:
    - args (list or None): Command line arguments, None to use sys.argv
    
    Features:
    - Automatic format detection based on output
    - TTY vs file output handling
    - Multiple image factory support
    - Error correction level selection
    - Data optimization options
    """

Usage Example:

from qrcode.console_scripts import main

# Programmatic command-line interface
main(['Hello World', '--output', 'test.png'])
main(['--factory', 'svg', 'Hello SVG', '--output', 'test.svg'])
main(['--ascii', 'Terminal QR'])

ASCII Terminal Output

Methods for displaying QR codes directly in the terminal using ASCII characters.

class QRCode:
    def print_ascii(self, out=None, tty=False, invert=False):
        """
        Print QR code using ASCII characters.
        
        Parameters:
        - out (file-like or None): Output stream, defaults to sys.stdout
        - tty (bool): Force TTY color mode
        - invert (bool): Invert colors (solid <-> transparent)
        
        Notes:
        - Uses Unicode block characters for better display
        - Automatically inverts colors for TTY mode
        - Raises OSError if tty=True but output is not a TTY
        """

Usage Example:

import qrcode
import sys

qr = qrcode.QRCode()
qr.add_data('Hello ASCII')
qr.make()

# Print to terminal
qr.print_ascii()

# Print with color inversion
qr.print_ascii(invert=True)

# Print to file
with open('ascii_qr.txt', 'w') as f:
    qr.print_ascii(out=f)

# Force TTY colors (if terminal supports it)
if sys.stdout.isatty():
    qr.print_ascii(tty=True)

TTY Color Output

Enhanced terminal output using ANSI color codes for better visibility.

class QRCode:
    def print_tty(self, out=None):
        """
        Print QR code using TTY colors (ANSI escape codes).
        
        Parameters:
        - out (file-like or None): Output stream, defaults to sys.stdout
        
        Notes:
        - Uses ANSI escape codes for black/white blocks
        - Only works on TTY terminals
        - Raises OSError if output is not a TTY
        - Better visibility than ASCII on supported terminals
        """

Usage Example:

import qrcode
import sys

qr = qrcode.QRCode()
qr.add_data('Hello TTY')
qr.make()

# Print with TTY colors (only if terminal supports it)
if sys.stdout.isatty():
    try:
        qr.print_tty()
    except OSError:
        print("TTY colors not supported")
        qr.print_ascii()
else:
    print("Not a TTY, using ASCII")
    qr.print_ascii()

Matrix Data Access

Methods for accessing raw QR code matrix data for custom output formats.

class QRCode:
    def get_matrix(self):
        """
        Get the QR code as a 2D boolean matrix.
        
        Returns:
        List[List[bool]]: 2D matrix where True = black module, False = white
        
        Notes:
        - Includes border if border > 0
        - Set border=0 for matrix without border
        - Must call make() first
        """

Usage Example:

import qrcode

qr = qrcode.QRCode(border=2)
qr.add_data('Matrix Data')
qr.make()

matrix = qr.get_matrix()

# Custom ASCII output
for row in matrix:
    print(''.join('██' if cell else '  ' for cell in row))

# Export as CSV
import csv
with open('qr_matrix.csv', 'w', newline='') as f:
    writer = csv.writer(f)
    for row in matrix:
        writer.writerow([1 if cell else 0 for cell in row])

# Convert to numpy array
import numpy as np
np_matrix = np.array(matrix, dtype=int)
print(f"QR code dimensions: {np_matrix.shape}")

Factory Selection for Console

Command-line factory selection and available factories.

# Built-in factory shortcuts for command line
default_factories = {
    "pil": "qrcode.image.pil.PilImage",
    "png": "qrcode.image.pure.PyPNGImage", 
    "svg": "qrcode.image.svg.SvgImage",
    "svg-fragment": "qrcode.image.svg.SvgFragmentImage",
    "svg-path": "qrcode.image.svg.SvgPathImage",
}

# Error correction mapping for command line
error_correction = {
    "L": qrcode.ERROR_CORRECT_L,    # ~7%
    "M": qrcode.ERROR_CORRECT_M,    # ~15% (default)
    "Q": qrcode.ERROR_CORRECT_Q,    # ~25%  
    "H": qrcode.ERROR_CORRECT_H,    # ~30%
}

Command Line Examples:

# Use different factories
qr "Hello" --factory pil --output image.png
qr "Hello" --factory svg --output image.svg  
qr "Hello" --factory svg-path --output compact.svg
qr "Hello" --factory png --output pure.png

# Error correction levels
qr "Hello" --error-correction L --output low_ec.png
qr "Hello" --error-correction H --output high_ec.png

# Optimization settings
qr "Hello123ABC" --optimize 5 --output optimized.png
qr "Long text data" --optimize 0 --output unoptimized.png  # Disable optimization

Cross-Platform Terminal Support

Automatic handling of terminal differences across operating systems.

# Windows terminal color support
import sys
if sys.platform.startswith(("win", "cygwin")):
    import colorama
    colorama.init()  # Automatically initialized for Windows

Platform-Specific Behavior:

  • Windows: Automatically initializes colorama for ANSI color support
  • Unix/Linux/macOS: Uses native ANSI color support
  • All platforms: Falls back to ASCII if colors not supported

Usage Example:

import qrcode
import sys

qr = qrcode.QRCode()
qr.add_data('Cross Platform')
qr.make()

# Smart terminal detection
if sys.stdout.isatty():
    try:
        qr.print_tty()  # Try color output first
    except OSError:
        qr.print_ascii(tty=True)  # Fall back to ASCII with TTY formatting
else:
    # File output or pipe
    if hasattr(sys.stdout, 'buffer'):
        # Binary output (image)
        img = qr.make_image()
        img.save(sys.stdout.buffer)
    else:
        # Text output
        qr.print_ascii()

Output Format Detection

Automatic format detection based on output destination and parameters.

# Automatic output format selection logic:
# 1. If --ascii flag: ASCII text output
# 2. If stdout is TTY: ASCII or TTY color output  
# 3. If stdout is pipe/file: Image binary output
# 4. If --output specified: Save to file

def auto_output_format(output_file, is_tty, ascii_mode, image_factory):
    """
    Determine output format based on context.
    
    Parameters:
    - output_file (str or None): Specified output file
    - is_tty (bool): Whether stdout is a terminal
    - ascii_mode (bool): Force ASCII mode
    - image_factory: Selected image factory
    
    Returns:
    str: Output mode ('ascii', 'tty', 'image')
    """

Format Selection Examples:

# ASCII to terminal (detected automatically)
qr "Hello" 
# Output: ASCII art to terminal

# Force ASCII even when piping
qr "Hello" --ascii > output.txt
# Output: ASCII art to file

# Image to file
qr "Hello" --output image.png
# Output: PNG image file

# Image to stdout (pipe)
qr "Hello" > image.png
# Output: Binary image data to stdout

# SVG to stdout  
qr "Hello" --factory svg
# Output: SVG text to stdout

Install with Tessl CLI

npx tessl i tessl/pypi-qrcode@7.4.0
What are skills?

docs

console-interface.md

constants.md

core-generation.md

image-formats.md

index.md

styling.md

tile.json