Advanced Topics
This guide covers advanced features and optimization techniques.
High Resolution Mode
Understanding Resolution
Each printer supports two resolution modes:
Standard resolution: 180 DPI (E550W, P750W) or 360 DPI (P900 series)
High resolution: 360 DPI (E550W, P750W) or 720 DPI (P900 series)
High resolution mode doubles the vertical resolution, resulting in:
Sharper text and graphics
Smoother curves and diagonals
~2x slower printing
Same tape length used
When to Use High Resolution
Use high resolution for:
Small text (< 12pt)
QR codes and barcodes
Detailed logos or graphics
Professional-looking labels
Standard resolution is sufficient for:
Large text (> 18pt)
Simple graphics
Cable labels and asset tags
When speed matters
Enabling High Resolution
# At printer initialization (applies to all prints)
printer = PTP900(connection, high_resolution=True)
# Per print job
printer = PTP900(connection)
printer.print(label, high_resolution=True)
# Command line
ptouch "High Quality" --high-resolution --host 192.168.1.100 \
--printer P900 --tape-width 36
TIFF Compression
How It Works
The library uses PackBits compression (TIFF) to reduce data transfer:
Compresses each raster line independently
Typical compression ratio: 50-80% for text labels
Especially effective for labels with large white areas
Minimal CPU overhead
Performance Impact
import time
# With compression (default)
start = time.time()
printer.print(label, use_compression=True)
compressed_time = time.time() - start
# Without compression
start = time.time()
printer.print(label, use_compression=False)
uncompressed_time = time.time() - start
print(f"Compression saved {uncompressed_time - compressed_time:.2f}s")
Disabling Compression
Disable if encountering issues or for troubleshooting:
printer.print(label, use_compression=False)
Lazy Connection Initialization
Connections are established lazily on first use:
# Connection object created, but not yet connected
connection = ConnectionNetwork("192.168.1.100")
# Actual connection happens here
printer = PTP900(connection)
printer.print(label) # Connects if needed
This pattern allows:
Fast initialization
Error handling at print time
Connection pooling strategies
Manual Connection Management
from ptouch import ConnectionNetwork, PrinterTimeoutError
connection = ConnectionNetwork("192.168.1.100")
printer = PTP900(connection)
# Retry logic
max_retries = 3
for attempt in range(max_retries):
try:
printer.print(label)
break
except PrinterTimeoutError:
if attempt == max_retries - 1:
raise
print(f"Retry {attempt + 1}/{max_retries}...")
time.sleep(1)
Image Processing
Optimal Image Format
For best results:
Format: PNG or JPEG
Color mode: RGB or Grayscale (converted to 1-bit internally)
Resolution: Match printer DPI (360 or 720)
Height: Match tape’s printable area
Calculating Image Dimensions
def calculate_dimensions(tape_width_mm, label_width_mm, dpi=360):
"""Calculate image dimensions for given tape and label width."""
# Printable area varies by tape width
printable_areas = {
6: 32, # 32 pins / ~2.3mm
9: 50, # 50 pins / ~3.5mm
12: 70, # 70 pins / ~4.9mm
18: 112, # 112 pins / ~7.9mm
24: 128, # 128 pins / ~9.0mm
36: 454, # 454 pins / ~31.8mm (at 360 DPI)
}
height_pixels = printable_areas.get(tape_width_mm, 128)
width_pixels = int(label_width_mm * dpi / 25.4)
return width_pixels, height_pixels
# Create optimally sized image
width, height = calculate_dimensions(36, 60, dpi=360)
img = Image.new("RGB", (width, height), "white")
Image Dithering
For grayscale images, apply dithering for better results:
from PIL import Image
# Load grayscale image
img = Image.open("photo.jpg").convert("L")
# Apply Floyd-Steinberg dithering
img = img.convert("1", dither=Image.FLOYDSTEINBERG)
# Print
label = Label(img.convert("RGB"), Tape36mm)
printer.print(label)
Batch Processing Optimization
Efficient Multi-Label Printing
def print_labels_efficiently(printer, label_data):
"""Print multiple labels with optimized settings."""
labels = []
# Pre-generate all labels
for data in label_data:
label = create_label(data) # Your label creation function
labels.append(label)
# Print in single job with half-cuts
printer.print_multi(labels, half_cut=True)
Network Optimization
Connection Timeout Tuning
import socket
connection = ConnectionNetwork("192.168.1.100")
# Access underlying socket (after connection established)
printer = PTP900(connection)
printer.print(first_label) # Establishes connection
# Adjust timeout
if hasattr(connection, '_socket'):
connection._socket.settimeout(30.0) # 30 second timeout
Keep-Alive for Long Sessions
def print_with_keepalive(printer, labels):
"""Print with connection keep-alive."""
for idx, label in enumerate(labels):
try:
printer.print(label)
except PrinterTimeoutError:
# Reconnect and retry
print(f"Reconnecting at label {idx}...")
printer = PTP900(ConnectionNetwork("192.168.1.100"))
printer.print(label)
Custom Raster Data
Advanced: Direct Raster Control
For complete control over raster data:
from ptouch.printer import LabelPrinter
# Access internal methods (use with caution)
tape_config = printer.get_tape_config(Tape36mm)
# Generate custom raster data
# Each line must be tape_config.print_pins bits
raster_data = generate_custom_raster() # Your function
# Build and send raster commands
# Note: This is internal API and may change
commands = printer._build_raster_data(
raster_data,
num_lines=len(raster_data) // tape_config.bytes_per_line,
high_res=True
)
Best Practices
Performance Tips
Reuse connections - Don’t create new connections per label
Batch prints - Use
print_multi()when printing multiple labelsPre-generate labels - Separate generation from printing
Use standard resolution - Unless quality demands high-res
Enable compression - Faster data transfer (default)
Optimize images - Match printer DPI, use 1-bit color when possible
Quality Tips
Use high resolution - For small text and detailed graphics
Test fonts - Not all fonts render well at all sizes
Proper DPI - Match image resolution to printer
Consider tape width - Larger tape = more detail possible
Apply dithering - For grayscale/photo images
Reliability Tips
Always handle errors - Network/USB can fail
Implement retries - For transient failures
Verify tape type - Wrong configuration causes misalignment
Check printer status - Before large batch jobs
Test first - Print one label before batch job
Next Steps
Adding Support for New Devices - Extend library with new printers
Troubleshooting - Solve common problems
Printer Module - API reference documentation