action
All checks were successful
Deploy WebSocket Server / deploy (push) Successful in 18s

This commit is contained in:
jeremygan2021
2026-03-05 20:56:21 +08:00
parent 6a64c54cae
commit a784c88c60
5 changed files with 108 additions and 10 deletions

View File

@@ -70,13 +70,13 @@ def image_to_tspl_commands(image_path):
for x in range(target_width):
pixel = img.getpixel((x, y))
should_print = False
# 修正逻辑:用户反馈打印出来是负片(黑底白字),说明原有的判断反了。
# 我们现在采用完全相反的逻辑。
# 之前的逻辑: if pixel != 0: should_print = True (导致背景被打印)
# 现在的逻辑: if pixel == 0: should_print = True (即 0(黑)打印255(白)不打印)
# 逻辑修正:
# 我们希望 黑色像素(0) -> 打印(1)
# 白色像素(255) -> 不打印(0)
# 使用 < 128 判定,增加容错性,防止像素值偏移
if pixel == 0:
should_print = False
if pixel < 128: # Black or Dark Gray
should_print = True
if should_print:

27
debug_dither.py Normal file
View File

@@ -0,0 +1,27 @@
from PIL import Image, ImageDraw
# Create a test image with gray lines
img = Image.new('L', (100, 100), color=255)
d = ImageDraw.Draw(img)
d.line([10, 10, 90, 90], fill=128, width=2) # Gray line
d.line([10, 90, 90, 10], fill=0, width=2) # Black line
# Convert with default dithering
img1 = img.convert('1')
zeros1 = 0
for y in range(100):
for x in range(100):
if img1.getpixel((x, y)) == 0: zeros1 += 1
print(f"Default dither zeros: {zeros1}")
# Convert with NO dithering (Threshold)
# Note: convert('1', dither=Image.Dither.NONE) might still do dithering in some versions?
# The reliable way to threshold is point operation or custom threshold.
# But let's check convert('1', dither=0)
img2 = img.convert('1', dither=Image.Dither.NONE)
zeros2 = 0
for y in range(100):
for x in range(100):
if img2.getpixel((x, y)) == 0: zeros2 += 1
print(f"No dither zeros: {zeros2}")

69
debug_img.py Normal file
View File

@@ -0,0 +1,69 @@
from PIL import Image
import os
def debug_image(image_path):
if not os.path.exists(image_path):
print(f"Error: {image_path} not found")
return
img = Image.open(image_path)
print(f"Original mode: {img.mode}")
# 模拟 convert_img.py 的处理流程
if img.mode in ('RGBA', 'LA') or (img.mode == 'P' and 'transparency' in img.info):
alpha = img.convert('RGBA').split()[-1]
bg = Image.new("RGBA", img.size, (255, 255, 255, 255))
bg.paste(img, mask=alpha)
img = bg
img = img.convert('L')
img = img.convert('1')
print(f"Converted mode: {img.mode}")
width, height = img.size
print(f"Size: {width}x{height}")
zeros = 0
non_zeros = 0
values = {}
# 采样一些像素
for y in range(min(height, 10)):
row_vals = []
for x in range(min(width, 10)):
val = img.getpixel((x, y))
row_vals.append(str(val))
print(f"Row {y} first 10 pixels: {', '.join(row_vals)}")
# 统计所有像素
for y in range(height):
for x in range(width):
val = img.getpixel((x, y))
if val == 0:
zeros += 1
else:
non_zeros += 1
if val not in values:
values[val] = 0
values[val] += 1
print(f"Total pixels: {width*height}")
print(f"Zeros (Black?): {zeros}")
print(f"Non-zeros (White?): {non_zeros}")
print(f"Non-zero values distribution: {values}")
if __name__ == "__main__":
# 尝试找一个存在的图片,或者创建一个
img_path = "test_image.png"
if not os.path.exists(img_path):
# 创建一个简单的测试图片
img = Image.new('RGB', (100, 100), color = 'white')
# 画个黑框
from PIL import ImageDraw
d = ImageDraw.Draw(img)
d.rectangle([10, 10, 90, 90], outline='black', fill='black')
img.save("debug_test.png")
img_path = "debug_test.png"
print("Created debug_test.png")
debug_image(img_path)

View File

@@ -57,9 +57,10 @@ def image_to_tspl_commands(image_path):
# 逻辑修正:
# 我们希望 黑色像素(0) -> 打印(1)
# 白色像素(255) -> 不打印(0)
# 使用 < 128 判定,增加容错性,防止像素值偏移
should_print = False
if pixel == 0: # Black
if pixel < 128: # Black or Dark Gray
should_print = True
if should_print:

View File

@@ -569,8 +569,8 @@ def optimize_prompt(asr_text, progress_callback=None):
1. 风格必须是:简单的黑白线稿、简笔画、图标风格 (Line art, Sketch, Icon style)。
2. 画面必须清晰、线条粗壮,适合低分辨率热敏打印机打印。
3. 绝对不要有复杂的阴影、渐变、黑白线条描述。
4. 背景必须是纯白 (White background)。
5. 提示词内容请使用英文描述,因为绘图模型对英文理解更好,但在描述中强调 "black and white line art", "simple lines", "vector style"
4. 背景必须是纯白 (White background),线条要粗方便打印
5. 提示词内容请使用英文描述,因为绘图模型对英文理解更好,但在描述中强调 "black and white line art", "bold simple lines", "vector style"
6. 尺寸比例遵循宽48mm:高30mm (约 1.6:1)。
7. 直接输出优化后的提示词,不要包含任何解释。"""
@@ -1025,4 +1025,5 @@ if __name__ == "__main__":
local_ip = socket.gethostbyname(hostname)
print(f"Server running on ws://{local_ip}:8000/ws/audio")
uvicorn.run(app, host="0.0.0.0", port=8000)
# 禁用自动Ping以避免并发写入冲突 (ws_ping_interval=None)
uvicorn.run(app, host="0.0.0.0", port=8000, ws_ping_interval=None, ws_ping_timeout=None)