52 lines
1.6 KiB
Python
52 lines
1.6 KiB
Python
"""
|
|
OCR script to extract text from screen.png
|
|
"""
|
|
import sys
|
|
import os
|
|
|
|
image_path = r"D:\Yuzu-GCA\screen.png"
|
|
|
|
# Try pytesseract first
|
|
try:
|
|
import pytesseract
|
|
from PIL import Image
|
|
img = Image.open(image_path)
|
|
text = pytesseract.image_to_string(img, lang='eng+chi_sim')
|
|
print("=== pytesseract OCR result ===")
|
|
print(text)
|
|
sys.exit(0)
|
|
except ImportError:
|
|
print("pytesseract not available, trying alternative...")
|
|
|
|
# Try Windows OCR via winrt
|
|
try:
|
|
import asyncio
|
|
from winsdk.windows.media.ocr import OcrEngine
|
|
from winsdk.windows.graphics.imaging import BitmapDecoder
|
|
from winsdk.windows.storage.streams import RandomAccessStreamReference
|
|
from winsdk.windows.globalization import Language
|
|
|
|
async def ocr_windows():
|
|
# Get the default OCR engine for Chinese
|
|
engine = OcrEngine.try_create_from_user_profile_languages()
|
|
if not engine:
|
|
# Fallback to English
|
|
engine = OcrEngine.try_create_from_language(Language("en"))
|
|
|
|
# Open the image file
|
|
file_stream = await RandomAccessStreamReference.create_from_file(image_path).open_read_async()
|
|
decoder = await BitmapDecoder.create_async(file_stream)
|
|
bitmap = await decoder.get_software_bitmap_async()
|
|
|
|
result = await engine.recognize_async(bitmap)
|
|
print("=== Windows OCR result ===")
|
|
for line in result.lines:
|
|
print(line.text)
|
|
|
|
asyncio.run(ocr_windows())
|
|
sys.exit(0)
|
|
except ImportError:
|
|
print("winsdk not available either")
|
|
|
|
print("No OCR engine available. Please install pytesseract or Windows SDK.")
|