如何解决使用Raspberry Pi使用Python将图像发送到QL-800打印机时,如何解码此错误?
一切都安装得很好,以获得Brother打印机驱动程序的Raspberry Pi版本。我用了 https://support.brother.com/g/b/downloadtop.aspx?c=us&lang=en&prod=lpql800eus,我用过https://pypi.org/project/brother-ql/
使用回溯,我如何理解应该为变量“打印机”使用什么值?我认为这就是问题所在。
代码如下:
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,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 库还可以根据路径格式猜测后端。
您可以在此处查看模块源:
,谢谢大家。 使它起作用的是使用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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。