Skip to content

Instantly share code, notes, and snippets.

@wolfmanstout
Created July 29, 2020 06:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wolfmanstout/5e8a286176f432d006640e3c1c4b45c1 to your computer and use it in GitHub Desktop.
Save wolfmanstout/5e8a286176f432d006640e3c1c4b45c1 to your computer and use it in GitHub Desktop.
Demonstrates using Python winrt to run OCR. Requires Python 3.7+.
import asyncio
import os
import winrt
from PIL import Image
from winrt.windows.graphics.imaging import BitmapDecoder, BitmapPixelFormat, SoftwareBitmap
from winrt.windows.media.ocr import OcrEngine
from winrt.windows.storage import StorageFile, FileAccessMode
import winrt.windows.storage.streams as streams
async def load_image_file_native(file_path):
file = await StorageFile.get_file_from_path_async(os.fspath(file_path))
stream = await file.open_async(FileAccessMode.READ)
decoder = await BitmapDecoder.create_async(stream)
return await decoder.get_software_bitmap_async()
def load_image_file_pillow(file_path):
image = Image.open(file_path).convert("RGBA")
data_writer = streams.DataWriter()
bytes = image.tobytes()
data_writer.write_bytes(list(bytes))
bitmap = SoftwareBitmap(BitmapPixelFormat.RGBA8, image.width, image.height)
bitmap.copy_from_buffer(data_writer.detach_buffer())
return bitmap
async def run_ocr(bitmap):
engine = OcrEngine.try_create_from_user_profile_languages()
return await engine.recognize_async(bitmap)
async def async_main():
screenshot_path = r"C:\Users\james\Documents\OCR\logs\failure_1595912159.34.png"
# bitmap = await load_image_file_native(screenshot_path)
bitmap = load_image_file_pillow(screenshot_path)
result = await run_ocr(bitmap)
print(result.text)
asyncio.run(async_main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment