本文目录导读:

我来帮你创建一个自动截图并识别文字(OCR)的脚本,这里提供两种常用方案:
Python脚本(推荐)
安装依赖
# 安装所需库 pip install pyautogui pillow pytesseract keyboard # Windows还需安装Tesseract OCR引擎 # 下载地址:https://github.com/UB-Mannheim/tesseract/wiki
Python脚本代码
import pyautogui
import pytesseract
from PIL import Image
import time
import keyboard
import os
from datetime import datetime
# 配置Tesseract路径(Windows需要)
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
class ScreenCaptureOCR:
def __init__(self):
self.screenshot_dir = "screenshots"
if not os.path.exists(self.screenshot_dir):
os.makedirs(self.screenshot_dir)
def capture_screen(self, region=None):
"""
截图功能
region: (x, y, width, height) 可选,指定截图区域
"""
if region:
screenshot = pyautogui.screenshot(region=region)
else:
screenshot = pyautogui.screenshot()
# 保存截图
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
filename = f"{self.screenshot_dir}/screenshot_{timestamp}.png"
screenshot.save(filename)
print(f"截图已保存: {filename}")
return screenshot, filename
def extract_text(self, image):
"""
识别图片中的文字
"""
# 中文识别
text = pytesseract.image_to_string(image, lang='chi_sim+eng')
return text.strip()
def auto_capture_and_ocr(self, interval=5, region=None):
"""
自动截图并识别文字
interval: 截图间隔(秒)
region: 截图区域
"""
print(f"开始自动截图识别(每{interval}秒一次),按 'q' 停止...")
try:
while True:
if keyboard.is_pressed('q'):
print("停止截图识别")
break
# 截图
screenshot, filename = self.capture_screen(region)
# 识别文字
text = self.extract_text(screenshot)
if text:
print(f"识别到的文字:\n{text}")
# 保存识别结果
text_filename = filename.replace('.png', '.txt')
with open(text_filename, 'w', encoding='utf-8') as f:
f.write(text)
print(f"文字已保存: {text_filename}")
else:
print("未识别到文字")
print("-" * 50)
time.sleep(interval)
except KeyboardInterrupt:
print("程序被用户中断")
def single_capture_and_ocr(self, region=None):
"""
单次截图并识别文字
"""
screenshot, filename = self.capture_screen(region)
text = self.extract_text(screenshot)
print(f"截图已保存: {filename}")
print(f"识别结果:\n{text}")
return text
def main():
ocr = ScreenCaptureOCR()
print("选择模式:")
print("1. 单次截图识别")
print("2. 自动定时截图识别")
print("3. 手动截图识别(按Ctrl+Shift+S)")
choice = input("请选择 (1/2/3): ")
if choice == '1':
# 单次截图
print("即将截图,请切换到目标窗口...")
time.sleep(3)
ocr.single_capture_and_ocr()
elif choice == '2':
# 自动定时截图
try:
interval = int(input("设置截图间隔(秒): ") or 5)
except ValueError:
interval = 5
ocr.auto_capture_and_ocr(interval=interval)
elif choice == '3':
# 手动快捷键截图
print("按下 Ctrl+Shift+S 进行截图,按 Q 退出...")
def on_hotkey():
print("截图快捷键触发,请选择区域...")
# 这里可以添加区域选择功能
screenshot, filename = ocr.capture_screen()
text = ocr.extract_text(screenshot)
print(f"识别结果:{text}")
keyboard.add_hotkey('ctrl+shift+s', on_hotkey)
keyboard.wait('q')
else:
print("无效选择")
if __name__ == "__main__":
main()
更简单的版本(适合快速使用)
import pyautogui
import pytesseract
from PIL import Image
import time
# 配置Tesseract路径(Windows需要)
# pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
def quick_ocr():
"""快速截图识别"""
print("3秒后自动截图...")
time.sleep(3)
# 全屏截图
screenshot = pyautogui.screenshot()
# 保存截图
screenshot.save('temp_screenshot.png')
# 识别文字
text = pytesseract.image_to_string(screenshot, lang='chi_sim+eng')
print("识别结果:")
print(text)
return text
if __name__ == "__main__":
quick_ocr()
使用百度OCR API(更准确)
import pyautogui
import requests
import base64
import time
import json
# 百度OCR API配置
API_KEY = "你的API_KEY"
SECRET_KEY = "你的SECRET_KEY"
def get_access_token():
"""获取百度OCR的access token"""
url = f"https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id={API_KEY}&client_secret={SECRET_KEY}"
response = requests.post(url)
return response.json().get('access_token')
def baidu_ocr(image_path):
"""使用百度OCR识别文字"""
token = get_access_token()
url = f"https://aip.baidubce.com/rest/2.0/ocr/v1/general_basic?access_token={token}"
with open(image_path, 'rb') as f:
image = base64.b64encode(f.read()).decode('utf-8')
headers = {'Content-Type': 'application/x-www-form-urlencoded'}
data = {'image': image}
response = requests.post(url, headers=headers, data=data)
result = response.json()
if 'words_result' in result:
text = '\n'.join([item['words'] for item in result['words_result']])
return text
else:
return "识别失败"
def main():
print("3秒后截图并识别...")
time.sleep(3)
# 截图
screenshot = pyautogui.screenshot()
screenshot.save('ocr_temp.png')
# OCR识别
text = baidu_ocr('ocr_temp.png')
print("识别结果:")
print(text)
if __name__ == "__main__":
main()
使用说明
-
安装Tesseract OCR(方案一和二需要):
- Windows:下载安装包并配置路径
- 语言包包含中文识别
-
配置API(方案三需要):
- 在百度AI平台注册应用
- 获取API Key和Secret Key
-
运行脚本:
python screen_ocr.py
功能特点
- ✅ 自动截图
- ✅ 文字识别(支持中文)
- ✅ 保存截图和识别结果
- ✅ 多种模式选择
- ✅ 可设置截图频率
- ✅ 快捷键支持
选择适合你需求的方案,如果需要更多定制化功能,请告诉我!
版权声明:NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
足球直播
足球直播
足球直播
篮球直播
篮球直播
篮球直播
NBA直播
NBA直播
NBA直播
CBA直播
CBA直播
CBA直播
亚冠直播
亚冠直播
亚冠直播
欧冠直播
欧冠直播
欧冠直播
英超直播
英超直播
英超直播
西甲直播
西甲直播
西甲直播
中超直播
中超直播
中超直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
曼联直播
曼联直播
曼联直播
日职联直播
日职联直播
日职联直播
澳超直播
澳超直播
澳超直播
阿森纳直播
阿森纳直播
阿森纳直播
足球直播
足球直播
足球直播
NBA直播
NBA直播
NBA直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
欧冠直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
足球直播
足球直播
足球直播
足球直播
足球直播
英超直播
英超直播
英超直播
英超直播
英超直播
篮球直播
篮球直播
篮球直播
篮球直播
篮球直播
CBA直播
CBA直播
CBA直播
CBA直播
CBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
NBA直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播
中超直播