import sys sys.path.insert(0, '.') from PIL import Image, ImageOps, ImageEnhance from winrt.windows.media.ocr import OcrEngine from winrt.windows.globalization import Language from winrt.windows.graphics.imaging import SoftwareBitmap, BitmapPixelFormat def ocr_image(path): im = Image.open(path).convert('RGBA') sb = SoftwareBitmap(BitmapPixelFormat.RGBA8, im.width, im.height) sb.copy_from_buffer(im.tobytes()) eng = OcrEngine.try_create_from_language(Language('zh-Hans-CN')) if not eng: eng = OcrEngine.try_create_from_user_profile_languages() res = eng.recognize_async(sb).get() return res.text im = Image.open('img/raw/qq/ocr_debug_qq_20260818_162956.261.png').convert('RGB') # 整条消息区 y=380-740 x=0-739, 温和处理 for (x0,y0,x1,y1,name,scale) in [ (0,380,739,740,'bottom_full',2), (0,540,739,740,'ts_bottom',3), (0,555,430,665,'ts_msg',4), (0,665,739,740,'very_bottom',4), ]: crop = im.crop((x0,y0,x1,y1)) crop8 = crop.resize((crop.width*scale, crop.height*scale), Image.LANCZOS) p = f'img/raw/qq/_dbg_{name}.png' crop8.save(p) try: t = ocr_image(p) print(f'{name}: {t!r}') except Exception as e: print(f'{name}: ERROR {e}')