diff --git a/convert_img.py b/convert_img.py index d12604f..f26787d 100644 --- a/convert_img.py +++ b/convert_img.py @@ -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: diff --git a/websocket_server/server.py b/websocket_server/server.py index 4e26954..e6c5241 100644 --- a/websocket_server/server.py +++ b/websocket_server/server.py @@ -676,8 +676,17 @@ def generate_image(prompt, progress_callback=None, retry_count=0, max_retries=2) from PIL import Image img = Image.open(GENERATED_IMAGE_FILE) - # 缩小到THUMB_SIZE x THUMB_SIZE - img = img.resize((THUMB_SIZE, THUMB_SIZE), Image.LANCZOS) + # 缩小到THUMB_SIZE x THUMB_SIZE,保持比例并居中(防止拉伸) + img.thumbnail((THUMB_SIZE, THUMB_SIZE), Image.LANCZOS) + + # 创建黑色背景 (240x240) + bg = Image.new("RGB", (THUMB_SIZE, THUMB_SIZE), (0, 0, 0)) + + # 计算居中位置 + left = (THUMB_SIZE - img.width) // 2 + top = (THUMB_SIZE - img.height) // 2 + bg.paste(img, (left, top)) + img = bg # 转换为RGB565格式的原始数据 # 每个像素2字节 (R5 G6 B5)