printer
All checks were successful
Deploy WebSocket Server / deploy (push) Successful in 19s

This commit is contained in:
jeremygan2021
2026-03-05 20:36:13 +08:00
parent ea0594bf88
commit 409b69b633
2 changed files with 17 additions and 22 deletions

View File

@@ -65,32 +65,18 @@ def image_to_tspl_commands(image_path):
data = bytearray()
# 遍历像素生成数据
# 修正逻辑:用户反馈打印出来是负片(黑底白字),说明原有的判断反了。
# 我们现在采用完全相反的逻辑。
# 之前的逻辑: if pixel == 0: should_print = True (导致背景被打印)
# 现在的逻辑: if pixel != 0: should_print = True (即 0不打印255打印)
# 这样背景(通常是0或255看PIL怎么处理)会被反转。
# 如果原图是白底(255)黑字(0)PIL convert('1') 后背景通常是 255字是 0。
# 如果用 if pixel != 0: print -> 背景打(黑),字不打(白)。这还是负片。
# 如果之前的逻辑 (pixel==0 -> print) 导致了负片,说明背景走了 print 分支,说明背景是 0。
# 这意味着 PIL 读进来的图背景是 0。
# 所以如果要背景不打我们应该if pixel == 0: no_print.
# 所以 if pixel != 0: print.
# 让我们再加一道保险ImageOps.invert
# 但 ImageOps.invert 不支持 '1' 模式,要先转 'L' 再 invert 再转 '1' 比较好。
# 不过既然我们是在 pixel 级别操作,我们可以直接在判断里改。
for y in range(target_height):
row_bytes = bytearray(width_bytes)
for x in range(target_width):
pixel = img.getpixel((x, y))
should_print = False
# 尝试修复:如果 pixel != 0 (即白色/非黑),则打印?
# 不,根据推导,背景如果是 0那我们要背景不打印所以 if pixel != 0: print.
# 让我们试试这个逻辑。
if pixel != 0:
# 修正逻辑:用户反馈打印出来是负片(黑底白字),说明原有的判断反了。
# 我们现在采用完全相反的逻辑。
# 之前的逻辑: if pixel != 0: should_print = True (导致背景被打印)
# 现在的逻辑: if pixel == 0: should_print = True (即 0(黑)打印255(白)不打印)
if pixel == 0:
should_print = True
if should_print: