[python]代码库
import pyautogui
import pytesseract
import cv2
import numpy as np
import time
# 设置Tesseract命令路径
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def capture_chat_window(region=None):
# 截取屏幕
screenshot = pyautogui.screenshot(region=region)
screenshot = cv2.cvtColor(np.array(screenshot), cv2.COLOR_RGB2BGR)
return screenshot
def extract_text(image):
# 使用OCR提取文本
text = pytesseract.image_to_string(image, lang='chi_sim') # 使用简体中文
return text
def main():
# 设置聊天窗口的区域(左,上,宽,高)
chat_window_region = (100, 100, 800, 600) # 根据实际情况调整
while True:
# 捕获聊天窗口
image = capture_chat_window(region=chat_window_region)
# 提取文本
text = extract_text(image)
# 打印提取的文本
if text.strip(): # 只打印非空内容
print(text.strip())
# 等待一段时间再执行下一次捕获
time.sleep(5) # 每5秒捕获一次,可根据需要调整
if __name__ == "__main__":
main()
高级设计师
by: Python自学 发表于:2025-01-08 15:07:19 顶(0) | 踩(0) 回复
pip install pyautogui pytesseract opencv-python Pillow
回复评论