在 Python 中使用 Raspberry Pi 将图像发送到 QL-800 打印机时如何解码此错误?

How to decode this error when using Raspberry Pi to send an image to a QL-800 printer in Python?

安装一切正常以获得兄弟打印机驱动程序的 Raspberry Pi 版本。我用了 https://support.brother.com/g/b/downloadtop.aspx?c=us&lang=en&prod=lpql800eus and I used https://pypi.org/project/brother-ql/

使用回溯,我如何理解我应该为变量“printer”使用什么值?我认为这是问题所在。

代码如下:

import pygame
from PIL import Image
from brother_ql.conversion import convert
from brother_ql.backends.helpers import send
from brother_ql.raster import BrotherQLRaster
#####################################################################################################
### Test QR-800 Printer
#####################################################################################################
im = Image.open('QLtest.png')
im.resize((306, 991)) 

backend = 'pyusb'    # 'pyusb', 'linux_kernal', 'network'
model = 'QL-800' # your printer model.

# HERE IS WHERE THE PROBLEM HAPPENS
# The code said to use a value from the Windows usb driver filter of 'usb://0x04f9:0x209b'
# or if have a Raspberry Pi Zero, to use for Linux/Raspberry Pi '/dev/usb/lp0'.
# So I tried with '/dev/usb/lp0' but get error
printer = '/dev/usb/lp0'    

qlr = BrotherQLRaster(model)
qlr.exception_on_warning = True
instructions = convert(
    qlr=qlr, 
    images=[im],    #  Takes a list of file names or PIL objects.
    label='29x90', 
    rotate='90',    # 'Auto', '0', '90', '270'
    threshold=70.0,    # Black and white threshold in percent.
    dither=False, 
    compress=False, 
    red=False,    # Only True if using Red/Black 62 mm label tape.
    dpi_600=False, 
    lq=False,    # True for low quality.
    no_cut=False
)
send(instructions=instructions, printer_identifier=printer, backend_identifier=backend, blocking=True)

这是错误:

Traceback (most recent call last):
  File "test_printer.py", line 36, in <module>
    send(instructions=instructions, printer_identifier=printer, backend_identifier=backend, blocking=True)
  File "/home/pi/.local/lib/python3.5/site-packages/brother_ql/backends/helpers.py", line 57, in send
    printer = BrotherQLBackend(printer_identifier)
  File "/home/pi/.local/lib/python3.5/site-packages/brother_ql/backends/pyusb.py", line 79, in __init__
    vendor, product = int(vendor, 16), int(product, 16)
ValueError: invalid literal for int() with base 16: ''

(移动评论回答)

在您的代码中,您将打印机路径指定为 /dev/usb/lp0,但将后端指定为 pyusb。对于 USB,库需要像 'usb://0x04f9:0x209b' 这样的路径,这会导致您看到的错误。您拥有的路径 (/dev/usb/lp0) 表示 linux_kernel 后端。尝试相应地更新您的代码。

backend = 'linux_kernel'

brother_ql库也可以根据路径格式猜测后端

您可以在这里查看模块源:

https://github.com/pklaus/brother_ql/blob/142cf744d89a912df729bbf15d35468d780559df/brother_ql/backends/init.py

谢谢大家。 使它起作用的是使用 linux_kernel(而不是 linux_kernal,这是一个打字错误)。

这是适用于 QL-800 打印机的示例代码

import pygame
import time
from PIL import Image
from brother_ql.conversion import convert
from brother_ql.backends.helpers import send
from brother_ql.raster import BrotherQLRaster
#####################################################################################################
### Test QR-800 Printer
#####################################################################################################
QLtest = pygame.image.load('red_black313x156.png')
QLtest = pygame.transform.scale(QLtest,(1044,696)) 
pygame.image.save(QLtest,'RedBlack2_1044x696.png') 

# You will need to convert to PIL object or save to harddrive with pygame.image.save and read back.
# For the Red lable, you need to resize in pygame to AnyWidthx696.  Premium Kiosk uses 1044,696.
image = Image.open('RedBlack2_1044x696.png')

backend = 'linux_kernel'    # 'pyusb', 'linux_kernel', 'network'
model = 'QL-800' # your printer model.

# The author of the code said to use a value from the Windows usb driver filter of 'usb://0x04f9:0x209b'
# or if have a Raspberry Pi Zero, to use for Linux/Raspberry Pi '/dev/usb/lp0'.
printer = '/dev/usb/lp0'    

qlr = BrotherQLRaster(model)
qlr.exception_on_warning = True
'''
To make it work for different types of labels, you need to change the label parameter below.  Here are some examples:
For the 29mm x 90.3mm, use label = '29x90' and red=False and (306, 991) image
For the DK-2251 Black/Red badge, use label='62red' and red=True and (AnyWidth,696) image (note, Premium Kiosk are 1044,696) 
note, if you don't have red or black in your image, you will not see anything for the red/black labels.
To learn more or to use other labels, run these commands in the command line:
     export PATH="${PATH}:~/.local/bin" (just always need to do this first to reach brother_ql)
     brother_ql_create --help (to understand the options in the code below)
     brother_ql_info list-label-sizes (to tell python what label (called Name) and image size (called Printable px)
'''
badge = convert(
    qlr=qlr, 
    images=[image],    #  Takes a list of file names or PIL objects.
    label='62red', 
    rotate='90',    # 'Auto', '0', '90', '270'
    threshold=70.0,    # Black and white threshold in percent.
    dither=False, 
    compress=False, 
    red=True,    # Only True if using Red/Black 62 mm label tape.
    dpi_600=False, 
    lq=False,    # True for low quality.
    no_cut=False
)
send(instructions=badge, printer_identifier=printer, backend_identifier=backend, blocking=True)