You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

打字游戏程序换行时末尾单词缺失空格问题求助

问题描述

我正在开发一款针对打字游戏的自动打字程序,目前遇到一个问题:当程序切换到下一行时,上一行末尾的单词后不会添加必需的空格,导致程序无法继续运行。单行内容时程序运行正常,但换行时始终出现这个问题,尝试过多种方案都没解决。

当前实现代码:

from pynput.keyboard import Controller
import pyautogui
import pytesseract 
import time
from PIL import Image

colour = 12, 79, 150

time.sleep(3)

ssl = True
while True:
    
    ss = pyautogui.screenshot(region = (535, 475, 1000, 110))
    if ss.getpixel == colour:
        True
    elif ss.getpixel != colour:
              
        ss.save(r"C:\Users\there\OneDrive\Desktop\VS Code\TypeRacer\screenshot\ss.png")
        print("saved")

        keyboard = Controller()

        pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\Tesseract.exe"

        img = Image.open(r"C:\Users\there\OneDrive\Desktop\VS Code\TypeRacer\screenshot\ss.png")
        print("image opened")

        text = pytesseract.image_to_string(img)
        print("image is stringPaY")

        text = text.replace("1, 2, 3, 4, 5, 6, 7, 8, 9, 0, (, ), |, /, \\, [, ], ',", "")
        text = text.replace("T", "T")
        text = text.replace("W", "W")
        print("done replacing")

        x = text.split(" ")
        x = " ".join(x)
        
        typing = True
        while typing:
                
            for chars in x:
                pyautogui.write(chars)
                
            colour2 = 209, 131, 131
            ss2 = pyautogui.screenshot(region = (535, 475, 1000, 110))

            if ss2.getpixel == colour2:
                typing = False
                
            else:
                pass

    quit()

问题根源

  1. OCR文本换行处理缺失:Tesseract识别多行文本时,会保留换行符,但原代码未处理换行符,导致换行位置没有空格,上下行内容直接拼接。
  2. getpixel调用错误:原代码直接比较方法对象ss.getpixel和颜色元组,未传入坐标参数(如(0,0)),导致颜色判断逻辑完全失效,程序无法正确识别换行时机。
  3. 打字循环冗余:原代码会重复输入整行文本,反而可能干扰正常换行逻辑。

修复方案

1. 修正文本预处理逻辑

添加换行符转空格的处理,合并连续空格:

# 替换所有换行符为空格,合并连续空格
import re
text = text.replace('\n', ' ').replace('\r', ' ')
text = re.sub(r'\s+', ' ', text).strip()

2. 修复颜色判断逻辑

给getpixel传入具体坐标(比如截图左上角像素):

# 检查截图左上角(0,0)的像素颜色
if ss.getpixel((0, 0)) == colour:
    continue

3. 优化打字循环

只输入一次处理后的文本,通过短延迟循环检测换行触发:

# 单次输入处理后的文本
pyautogui.write(text)
# 等待当前行完成输入
while True:
    ss2 = pyautogui.screenshot(region=(535, 475, 1000, 110))
    if ss2.getpixel((0, 0)) == colour2:
        break
    time.sleep(0.1)

修改后的完整代码

from pynput.keyboard import Controller
import pyautogui
import pytesseract 
import time
from PIL import Image
import re

colour = (12, 79, 150)
colour2 = (209, 131, 131)

time.sleep(3)

# 提前初始化工具,避免循环内重复创建
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\Tesseract.exe"
keyboard = Controller()

while True:
    ss = pyautogui.screenshot(region=(535, 475, 1000, 110))
    # 检查当前区域是否需要识别
    if ss.getpixel((0, 0)) == colour:
        continue
    
    ss.save(r"C:\Users\there\OneDrive\Desktop\VS Code\TypeRacer\screenshot\ss.png")
    print("saved")
    
    img = Image.open(r"C:\Users\there\OneDrive\Desktop\VS Code\TypeRacer\screenshot\ss.png")
    print("image opened")
    
    text = pytesseract.image_to_string(img)
    print("image converted to string")
    
    # 文本预处理:清理特殊字符、处理换行、合并空格
    text = text.replace("1, 2, 3, 4, 5, 6, 7, 8, 9, 0, (, ), |, /, \\, [, ], ',", "")
    text = text.replace('\n', ' ').replace('\r', ' ')
    text = re.sub(r'\s+', ' ', text).strip()
    
    print("processed text:", text)
    
    # 输入文本
    pyautogui.write(text)
    
    # 等待当前行输入完成
    while True:
        ss2 = pyautogui.screenshot(region=(535, 475, 1000, 110))
        if ss2.getpixel((0, 0)) == colour2:
            break
        time.sleep(0.1)
    
    print("current line done, waiting for next line")

内容的提问来源于stack exchange,提问作者xSpacco

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.05 23:55:26