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

Posted

技术标签:

【中文标题】使用 Raspberry Pi 将图像发送到 Python 中的 QL-800 打印机时如何解码此错误?【英文标题】:How to decode this error when using Raspberry Pi to send an image to a QL-800 printer in Python? 【发布时间】:2020-08-31 01:24:42 【问题描述】:

为了获得兄弟打印机驱动程序的 Raspberry Pi 版本,一切都安装得很好。我用了 https://support.brother.com/g/b/downloadtop.aspx?c=us&lang=en&prod=lpql800eus 我用的是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: ''

【问题讨论】:

尝试将后端切换为“linux_kernal”或将其留空。 感谢 Mike67 试图帮助我。我尝试使用“linux_kernal”并收到一个新错误,提示“未实现后端 linux_kernal”。 Traceback(最近一次调用最后一次):文件“test_printer.py”,第 36 行,在 中发送(instructions=instructions,printer_identifier=printer,backend_identifier=backend,blocking=True)文件“ /home/pi/.local/lib/python3.5/site-packages/brother_ql/backends/helpers.py”,第 53 行,在发送 be = backend_factory(selected_backend) 文件“/home/pi/.local/lib/ python3.5/site-packages/brother_ql/backends/__init__.py",第 38 行,在 backend_factory 中引发 NotImplementedError('Backend %s not implemented.' % backend_name) NotImplementedError: Backend linux_kernal not implemented。 对不起 - 我的错字。试试linux_kernel 我非常喜欢堆栈溢出和人们。谢谢迈克67。你得到了与打印机对话的代码,所以我注意到我使用的标签纸与 python 代码编程的不匹配。所以我改变了纸张类型,它奏效了。好的,我现在才弄清楚python想要称之为DK-2251类型的纸,它应该是实心的。经过 4 天的奋斗,我无法告诉你我有多兴奋。完善后我会再次更新。 【参考方案1】:

(移动评论回答)

在您的代码中,您将打印机路径指定为/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

【讨论】:

【参考方案2】:

谢谢大家。 使它起作用的是使用 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)

【讨论】:

以上是关于使用 Raspberry Pi 将图像发送到 Python 中的 QL-800 打印机时如何解码此错误?的主要内容,如果未能解决你的问题,请参考以下文章

我如何让Raspberry Pi用python发送带有图片的电子邮件

将 HEX 值发送到 Raspberry PI B+ 上的 SPI

在 Raspberry Pi 中使用 OpenCV 和套接字通过 TCP 发送视频

如何使用 QT 在 Raspberry Pi 上的 LCD 和 HDMI 上同时在 Linux 中绘制图像?

在raspberry pi中安装缺少的python包

如何在 Raspberry Pi 上使用 C++ 将接收到的 UDP 音频数据正确写入 ALSA