import sys sys.path.insert(0, '.') from PIL import Image, ImageOps 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 for p in ['img/raw/qq/_dbg_msg4_blue.png','img/raw/qq/_dbg_msg4_black.png','img/raw/qq/_dbg_msg4_blue6.png']: try: print(f'{p}: {ocr_image(p)!r}') except Exception as e: print(f'{p}: ERROR {e}')